-
Notifications
You must be signed in to change notification settings - Fork 4
/
ccColorScaleEditorDlg.cpp
667 lines (564 loc) · 18.8 KB
/
ccColorScaleEditorDlg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
//##########################################################################
//# #
//# CLOUDCOMPARE #
//# #
//# This program is free software; you can redistribute it and/or modify #
//# it under the terms of the GNU General Public License as published by #
//# the Free Software Foundation; version 2 of the License. #
//# #
//# This program is distributed in the hope that it will be useful, #
//# but WITHOUT ANY WARRANTY; without even the implied warranty of #
//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
//# GNU General Public License for more details. #
//# #
//# COPYRIGHT: EDF R&D / TELECOM ParisTech (ENST-TSI) #
//# #
//##########################################################################
#include "ccColorScaleEditorDlg.h"
//local
#include "ccColorScaleEditorWidget.h"
#include "ccDisplayOptionsDlg.h"
#include "ccPersistentSettings.h"
//qCC_db
#include <ccColorScalesManager.h>
#include <ccScalarField.h>
//Qt
#include <QHBoxLayout>
#include <QColorDialog>
#include <QMessageBox>
#include <QInputDialog>
#include <QFileDialog>
#include <QSettings>
#include <QUuid>
//System
#include <assert.h>
ccColorScaleEditorDialog::ccColorScaleEditorDialog(ccColorScalesManager* manager, ccColorScale::Shared currentScale/*=0*/, QWidget* parent/*=0*/)
: QDialog(parent)
, Ui::ColorScaleEditorDlg()
, m_manager(manager)
, m_colorScale(currentScale)
, m_scaleWidget(new ccColorScaleEditorWidget(this,Qt::Horizontal))
, m_associatedSF(0)
, m_modified(false)
, m_minAbsoluteVal(0.0)
, m_maxAbsoluteVal(1.0)
{
assert(m_manager);
setupUi(this);
colorScaleEditorFrame->setLayout(new QHBoxLayout());
colorScaleEditorFrame->layout()->setContentsMargins(0,0,0,0);
colorScaleEditorFrame->layout()->addWidget(m_scaleWidget);
//main combo box
connect(rampComboBox, SIGNAL(activated(int)), this, SLOT(colorScaleChanged(int)));
//import/export buttons
connect(exportToolButton, SIGNAL(clicked()), this, SLOT(exportCurrentScale()));
connect(importToolButton, SIGNAL(clicked()), this, SLOT(importScale()));
//upper buttons
connect(renameToolButton, SIGNAL(clicked()), this, SLOT(renameCurrentScale()));
connect(saveToolButton, SIGNAL(clicked()), this, SLOT(saveCurrentScale()));
connect(deleteToolButton, SIGNAL(clicked()), this, SLOT(deleteCurrentScale()));
connect(copyToolButton, SIGNAL(clicked()), this, SLOT(copyCurrentScale()));
connect(newToolButton, SIGNAL(clicked()), this, SLOT(createNewScale()));
connect(scaleModeComboBox, SIGNAL(activated(int)), this, SLOT(relativeModeChanged(int)));
//scale widget
connect(m_scaleWidget, SIGNAL(stepSelected(int)), this, SLOT(onStepSelected(int)));
connect(m_scaleWidget, SIGNAL(stepModified(int)), this, SLOT(onStepModified(int)));
//slider editor
connect(deleteSliderToolButton, SIGNAL(clicked()), this, SLOT(deletecSelectedStep()));
connect(colorToolButton, SIGNAL(clicked()), this, SLOT(changeSelectedStepColor()));
connect(valueDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeSelectedStepValue(double)));
//close button
connect(closePushButton, SIGNAL(clicked()), this, SLOT(onClose()));
//populate main combox box with all known scales
updateMainComboBox();
if (!m_colorScale)
m_colorScale = m_manager->getDefaultScale(ccColorScalesManager::BGYR);
setActiveScale(m_colorScale);
}
ccColorScaleEditorDialog::~ccColorScaleEditorDialog()
{
}
void ccColorScaleEditorDialog::setAssociatedScalarField(ccScalarField* sf)
{
m_associatedSF = sf;
if (m_associatedSF && (!m_colorScale || m_colorScale->isRelative())) //we only update those values if the current scale is not absolute!
{
m_minAbsoluteVal = m_associatedSF->getMin();
m_maxAbsoluteVal = m_associatedSF->getMax();
}
}
void ccColorScaleEditorDialog::updateMainComboBox()
{
if (!m_manager)
{
assert(false);
return;
}
rampComboBox->blockSignals(true);
rampComboBox->clear();
//populate combo box with scale names (and UUID)
assert(m_manager);
for (ccColorScalesManager::ScalesMap::const_iterator it = m_manager->map().begin(); it != m_manager->map().end(); ++it)
rampComboBox->addItem((*it)->getName(),(*it)->getUuid());
//find the currently selected scale in the new 'list'
int pos = -1;
if (m_colorScale)
{
pos = rampComboBox->findData(m_colorScale->getUuid());
if (pos < 0) //the current color scale has disappeared?!
m_colorScale = ccColorScale::Shared(0);
}
rampComboBox->setCurrentIndex(pos);
rampComboBox->blockSignals(false);
}
void ccColorScaleEditorDialog::colorScaleChanged(int pos)
{
QString UUID = rampComboBox->itemData(pos).toString();
ccColorScale::Shared colorScale = ccColorScalesManager::GetUniqueInstance()->getScale(UUID);
setActiveScale(colorScale);
}
void ccColorScaleEditorDialog::relativeModeChanged(int value)
{
setScaleModeToRelative(value == 0 ? true : false);
setModified(true);
}
void ccColorScaleEditorDialog::setModified(bool state)
{
m_modified = state;
saveToolButton->setEnabled(m_modified);
}
bool ccColorScaleEditorDialog::canChangeCurrentScale()
{
if (!m_colorScale || !m_modified)
return true;
if (m_colorScale->isLocked())
{
assert(false);
return true;
}
///ask the user if we should save the current scale?
QMessageBox::StandardButton button = QMessageBox::warning(this,
"Current scale has been modified",
"Do you want to save modifications?",
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
QMessageBox::Cancel);
if (button == QMessageBox::Yes)
{
saveCurrentScale();
}
else if (button == QMessageBox::Cancel)
{
return false;
}
return true;
}
bool ccColorScaleEditorDialog::isRelativeMode() const
{
return (scaleModeComboBox->currentIndex() == 0 ? true : false);
}
void ccColorScaleEditorDialog::setActiveScale(ccColorScale::Shared currentScale)
{
//the user wants to change the current scale while the it has been modified (and potentially not saved)
if (m_colorScale != currentScale)
{
if (!canChangeCurrentScale())
{
//restore old combo-box state
int pos = rampComboBox->findData(m_colorScale->getUuid());
if (pos >= 0)
{
rampComboBox->blockSignals(true);
rampComboBox->setCurrentIndex(pos);
rampComboBox->blockSignals(false);
}
else
{
assert(false);
}
//stop process
return;
}
}
m_colorScale = currentScale;
setModified(false);
//make sure combo-box is up to date
{
int pos = rampComboBox->findData(m_colorScale->getUuid());
if (pos >= 0)
{
rampComboBox->blockSignals(true);
rampComboBox->setCurrentIndex(pos);
rampComboBox->blockSignals(false);
}
}
//setup dialog components
{
//locked state
bool isLocked = !m_colorScale || m_colorScale->isLocked();
colorScaleParametersFrame->setEnabled(!isLocked);
exportToolButton->setEnabled(!isLocked);
lockWarningLabel->setVisible(isLocked);
selectedSliderGroupBox->setEnabled(!isLocked);
m_scaleWidget->setEnabled(!isLocked);
//absolute or relative mode
if (m_colorScale)
{
bool isRelative = m_colorScale->isRelative();
if (!isRelative)
{
//absolute color scales defines their own boundaries
m_colorScale->getAbsoluteBoundaries(m_minAbsoluteVal,m_maxAbsoluteVal);
}
setScaleModeToRelative(isRelative);
}
else
{
//shouldn't be accessible anyway....
assert(isLocked == true);
setScaleModeToRelative(false);
}
}
m_scaleWidget->importColorScale(m_colorScale);
onStepSelected(-1);
}
void ccColorScaleEditorDialog::setScaleModeToRelative(bool isRelative)
{
scaleModeComboBox->setCurrentIndex(isRelative ? 0 : 1);
valueDoubleSpinBox->setSuffix(isRelative ? QString(" %") : QString());
valueDoubleSpinBox->blockSignals(true);
if (isRelative)
valueDoubleSpinBox->setRange(0.0,100.0); //between 0 and 100%
else
valueDoubleSpinBox->setRange(-1.0e9,1.0e9);
valueDoubleSpinBox->blockSignals(false);
//update selected slider frame
int selectedIndex = (m_scaleWidget ? m_scaleWidget->getSelectedStepIndex() : -1);
onStepModified(selectedIndex);
}
void ccColorScaleEditorDialog::onStepSelected(int index)
{
selectedSliderGroupBox->setEnabled(/*m_colorScale && !m_colorScale->isLocked() && */index >= 0);
deleteSliderToolButton->setEnabled(index >= 1 && index+1 < m_scaleWidget->getStepCount()); //don't delete the first and last steps!
if (index < 0)
{
valueDoubleSpinBox->blockSignals(true);
valueDoubleSpinBox->setValue(0.0);
valueDoubleSpinBox->blockSignals(false);
ccDisplayOptionsDlg::SetButtonColor(colorToolButton,Qt::gray);
valueLabel->setVisible(false);
}
else
{
bool modified = m_modified; //save 'modified' state before calling onStepModified (which will force it to true)
onStepModified(index);
setModified(modified); //restore true 'modified' state
}
}
void ccColorScaleEditorDialog::onStepModified(int index)
{
if (index < 0 || index >= m_scaleWidget->getStepCount())
return;
const ColorScaleElementSlider* slider = m_scaleWidget->getStep(index);
assert(slider);
ccDisplayOptionsDlg::SetButtonColor(colorToolButton,slider->getColor());
if (m_colorScale)
{
const double relativePos = slider->getRelativePos();
if (isRelativeMode())
{
valueDoubleSpinBox->blockSignals(true);
valueDoubleSpinBox->setValue(relativePos*100.0);
valueDoubleSpinBox->blockSignals(false);
if (m_associatedSF)
{
//compute corresponding scalar value for associated SF
double actualValue = m_associatedSF->getMin() + relativePos * (m_associatedSF->getMax() - m_associatedSF->getMin());
valueLabel->setText(QString("(%1)").arg(actualValue));
valueLabel->setVisible(true);
}
else
{
valueLabel->setVisible(false);
}
//can't change min and max boundaries in 'relative' mode!
valueDoubleSpinBox->setEnabled(index > 0 && index < m_scaleWidget->getStepCount()-1);
}
else
{
//compute corresponding 'absolute' value from current dialog boundaries
double absoluteValue = m_minAbsoluteVal + relativePos * (m_maxAbsoluteVal - m_minAbsoluteVal);
valueDoubleSpinBox->blockSignals(true);
valueDoubleSpinBox->setValue(absoluteValue);
valueDoubleSpinBox->blockSignals(false);
valueDoubleSpinBox->setEnabled(true);
//display corresponding relative position as well
valueLabel->setText(QString("(%1 %)").arg(relativePos*100.0));
valueLabel->setVisible(true);
}
setModified(true);
}
}
void ccColorScaleEditorDialog::deletecSelectedStep()
{
int selectedIndex = m_scaleWidget->getSelectedStepIndex();
if (selectedIndex >= 1 && selectedIndex+1 < m_scaleWidget->getStepCount()) //never delete the first and last steps!
{
m_scaleWidget->deleteStep(selectedIndex);
setModified(true);
}
}
void ccColorScaleEditorDialog::changeSelectedStepColor()
{
int selectedIndex = m_scaleWidget->getSelectedStepIndex();
if (selectedIndex < 0)
return;
const ColorScaleElementSlider* slider = m_scaleWidget->getStep(selectedIndex);
assert(slider);
QColor newCol = QColorDialog::getColor(slider->getColor(), this);
if (newCol.isValid())
{
//eventually onStepModified will be called (and thus m_modified will be updated)
m_scaleWidget->setStepColor(selectedIndex,newCol);
}
}
void ccColorScaleEditorDialog::changeSelectedStepValue(double value)
{
if (!m_scaleWidget)
return;
int selectedIndex = m_scaleWidget->getSelectedStepIndex();
if (selectedIndex < 0)
return;
const ColorScaleElementSlider* slider = m_scaleWidget->getStep(selectedIndex);
assert(slider);
bool relativeMode = isRelativeMode();
if (relativeMode)
{
assert(selectedIndex != 0 && selectedIndex+1 < m_scaleWidget->getStepCount());
value /= 100.0; //from percentage to relative position
assert(value >= 0.0 && value <= 1.0);
//eventually onStepModified will be called (and thus m_modified will be updated)
m_scaleWidget->setStepRelativePosition(selectedIndex,value);
}
else //absolute scale mode
{
//we build up the new list based on absolute values
SharedColorScaleElementSliders newSliders(new ColorScaleElementSliders());
{
for (int i=0;i<m_scaleWidget->getStepCount();++i)
{
const ColorScaleElementSlider* slider = m_scaleWidget->getStep(i);
double absolutePos = (i == selectedIndex ? value : m_minAbsoluteVal + slider->getRelativePos() * (m_maxAbsoluteVal - m_minAbsoluteVal));
newSliders->push_back(new ColorScaleElementSlider(absolutePos,slider->getColor()));
}
}
//update min and max boundaries
{
newSliders->sort();
m_minAbsoluteVal = newSliders->front()->getRelativePos(); //absolute in fact!
m_maxAbsoluteVal = newSliders->back()->getRelativePos(); //absolute in fact!
}
//convert absolute pos to relative ones
int newSelectedIndex = -1;
{
double range = std::max(m_maxAbsoluteVal-m_minAbsoluteVal,1e-12);
for (int i=0;i<newSliders->size();++i)
{
double absoluteVal = newSliders->at(i)->getRelativePos();
if (absoluteVal == value)
newSelectedIndex = i;
double relativePos = (absoluteVal-m_minAbsoluteVal)/range;
newSliders->at(i)->setRelativePos(relativePos);
}
}
//update the whole scale with new sliders
m_scaleWidget->setSliders(newSliders);
m_scaleWidget->setSelectedStepIndex(newSelectedIndex,true);
setModified(true);
}
}
void ccColorScaleEditorDialog::copyCurrentScale()
{
if (!m_colorScale)
{
assert(false);
return;
}
ccColorScale::Shared scale = ccColorScale::Create(m_colorScale->getName()+QString("_copy"));
if (!m_colorScale->isRelative())
{
double minVal,maxVal;
m_colorScale->getAbsoluteBoundaries(minVal,maxVal);
scale->setAbsolute(minVal,maxVal);
}
m_scaleWidget->exportColorScale(scale);
assert(m_manager);
if (m_manager)
m_manager->addScale(scale);
updateMainComboBox();
setActiveScale(scale);
}
void ccColorScaleEditorDialog::saveCurrentScale()
{
if (!m_colorScale || m_colorScale->isLocked())
{
assert(false);
return;
}
m_scaleWidget->exportColorScale(m_colorScale);
bool relativeMode = isRelativeMode();
if (relativeMode)
m_colorScale->setRelative();
else
m_colorScale->setAbsolute(m_minAbsoluteVal,m_maxAbsoluteVal);
setModified(false);
}
void ccColorScaleEditorDialog::renameCurrentScale()
{
if (!m_colorScale || m_colorScale->isLocked())
{
assert(false);
return;
}
QString newName = QInputDialog::getText(this,"Scale name","Name",QLineEdit::Normal,m_colorScale->getName());
if (!newName.isNull())
{
m_colorScale->setName(newName);
//position in combo box
int pos = rampComboBox->findData(m_colorScale->getUuid());
if (pos >= 0)
//update combo box entry name
rampComboBox->setItemText(pos,newName);
}
}
void ccColorScaleEditorDialog::deleteCurrentScale()
{
if (!m_colorScale || m_colorScale->isLocked())
{
assert(false);
return;
}
//ask for confirmation
if (QMessageBox::warning( this,
"Delete scale",
"Are you sure?",
QMessageBox::Yes | QMessageBox::No,
QMessageBox::No) == QMessageBox::No)
{
return;
}
//backup current scale
ccColorScale::Shared colorScaleToDelete = m_colorScale;
setModified(false); //cancel any modification
int currentIndex = rampComboBox->currentIndex();
if (currentIndex == 0)
currentIndex = 1;
else if (currentIndex > 0)
--currentIndex;
assert(m_manager);
if (m_manager)
{
//activate the neighbor scale in the list
ccColorScale::Shared nextScale = m_manager->getScale(rampComboBox->itemData(currentIndex).toString());
setActiveScale(nextScale);
m_manager->removeScale(colorScaleToDelete->getUuid());
}
updateMainComboBox();
}
void ccColorScaleEditorDialog::createNewScale()
{
ccColorScale::Shared scale = ccColorScale::Create("New scale");
//add default min and max steps
scale->insert(ccColorScaleElement(0.0,Qt::blue),false);
scale->insert(ccColorScaleElement(1.0,Qt::red),true);
assert(m_manager);
if (m_manager)
m_manager->addScale(scale);
updateMainComboBox();
setActiveScale(scale);
}
void ccColorScaleEditorDialog::onClose()
{
if (canChangeCurrentScale())
{
accept();
}
}
void ccColorScaleEditorDialog::exportCurrentScale()
{
if (!m_colorScale || m_colorScale->isLocked())
{
assert(false);
return;
}
//persistent settings
QSettings settings;
settings.beginGroup(ccPS::SaveFile());
QString currentPath = settings.value(ccPS::CurrentPath(),QApplication::applicationDirPath()).toString();
//ask for a filename
QString filename = QFileDialog::getSaveFileName(this,"Select output file",currentPath,"*.xml");
if (filename.isEmpty())
{
//process cancelled by user
return;
}
//save last saving location
settings.setValue(ccPS::CurrentPath(),QFileInfo(filename).absolutePath());
settings.endGroup();
//try to save the file
if (m_colorScale->saveAsXML(filename))
{
ccLog::Print(QString("[ColorScale] Scale '%1' sucessfully exported in '%2'").arg(m_colorScale->getName()).arg(filename));
}
}
void ccColorScaleEditorDialog::importScale()
{
//persistent settings
QSettings settings;
settings.beginGroup(ccPS::LoadFile());
QString currentPath = settings.value(ccPS::CurrentPath(),QApplication::applicationDirPath()).toString();
//ask for a filename
QString filename = QFileDialog::getOpenFileName(this,"Select color scale file",currentPath,"*.xml");
if (filename.isEmpty())
{
//process cancelled by user
return;
}
//save last loading parameters
settings.setValue(ccPS::CurrentPath(),QFileInfo(filename).absolutePath());
settings.endGroup();
//try to load the file
ccColorScale::Shared scale = ccColorScale::LoadFromXML(filename);
if (scale)
{
assert(m_manager);
if (m_manager)
{
ccColorScale::Shared otherScale = m_manager->getScale(scale->getUuid());
if (otherScale)
{
QString message = "A color scale with the same UUID";
if (otherScale->getName() == scale->getName())
message += QString(" and the same name (%1)").arg(scale->getName());
message += " is already in store!";
message += "\n";
message += "Do you want to force the importation of this new scale? (a new UUID will be generated)";
if (QMessageBox::question (this,
"UUID conflict",
message,
QMessageBox::Yes,
QMessageBox::No) == QMessageBox::No)
{
ccLog::Warning("[ccColorScaleEditorDialog::importScale] Importation cancelled due to a conflicting UUID (color scale may already be in store)");
return;
}
//generate a new UUID
scale->setUuid(QUuid::createUuid().toString());
}
//now we can import the scale
m_manager->addScale(scale);
ccLog::Print(QString("[ccColorScaleEditorDialog::importScale] Color scale '%1' successfully imported").arg(scale->getName()));
}
updateMainComboBox();
setActiveScale(scale);
}
}