/
Widgets.cpp
1706 lines (1452 loc) · 50.2 KB
/
Widgets.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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <QColorDialog>
# include <QDebug>
# include <QDesktopServices>
# include <QDialogButtonBox>
# include <QDrag>
# include <QEventLoop>
# include <QKeyEvent>
# include <QMessageBox>
# include <QMimeData>
# include <QPainter>
# include <QPlainTextEdit>
# include <QStylePainter>
# include <QTextBlock>
# include <QTimer>
# include <QToolTip>
#endif
#include <Base/Tools.h>
#include <Base/Exception.h>
#include <Base/Interpreter.h>
#include <App/ExpressionParser.h>
#include "Widgets.h"
#include "Action.h"
#include "Application.h"
#include "BitmapFactory.h"
#include "Command.h"
#include "DlgExpressionInput.h"
#include "PrefWidgets.h"
#include "QuantitySpinBox_p.h"
#include "Tools.h"
#include "ui_DlgTreeWidget.h"
using namespace Gui;
using namespace App;
using namespace Base;
/**
* Constructs an empty command view with parent \a parent.
*/
CommandIconView::CommandIconView ( QWidget * parent )
: QListWidget(parent)
{
connect(this, SIGNAL (currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
this, SLOT (onSelectionChanged(QListWidgetItem *, QListWidgetItem *)) );
}
/**
* Destroys the icon view and deletes all items.
*/
CommandIconView::~CommandIconView ()
{
}
/**
* Stores the name of the selected commands for drag and drop.
*/
void CommandIconView::startDrag (Qt::DropActions supportedActions)
{
Q_UNUSED(supportedActions);
QList<QListWidgetItem*> items = selectedItems();
QByteArray itemData;
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
QPixmap pixmap;
dataStream << items.count();
for (QList<QListWidgetItem*>::Iterator it = items.begin(); it != items.end(); ++it) {
if (it == items.begin())
pixmap = ((*it)->data(Qt::UserRole)).value<QPixmap>();
dataStream << (*it)->text();
}
auto mimeData = new QMimeData;
mimeData->setData(QString::fromLatin1("text/x-action-items"), itemData);
auto drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->setHotSpot(QPoint(pixmap.width()/2, pixmap.height()/2));
drag->setPixmap(pixmap);
drag->exec(Qt::MoveAction);
}
/**
* This slot is called when a new item becomes current. \a item is the new current item
* (or 0 if no item is now current). This slot emits the emitSelectionChanged()
* signal for its part.
*/
void CommandIconView::onSelectionChanged(QListWidgetItem * item, QListWidgetItem *)
{
if (item)
Q_EMIT emitSelectionChanged(item->toolTip());
}
// ------------------------------------------------------------------------------
/* TRANSLATOR Gui::ActionSelector */
ActionSelector::ActionSelector(QWidget* parent)
: QWidget(parent)
{
addButton = new QPushButton(this);
addButton->setObjectName(QLatin1String("addButton"));
addButton->setMinimumSize(QSize(30, 30));
addButton->setIcon(BitmapFactory().pixmap("button_right"));
gridLayout = new QGridLayout(this);
gridLayout->addWidget(addButton, 1, 1, 1, 1);
spacerItem = new QSpacerItem(33, 57, QSizePolicy::Minimum, QSizePolicy::Expanding);
gridLayout->addItem(spacerItem, 5, 1, 1, 1);
spacerItem1 = new QSpacerItem(33, 58, QSizePolicy::Minimum, QSizePolicy::Expanding);
gridLayout->addItem(spacerItem1, 0, 1, 1, 1);
removeButton = new QPushButton(this);
removeButton->setObjectName(QLatin1String("removeButton"));
removeButton->setMinimumSize(QSize(30, 30));
removeButton->setIcon(BitmapFactory().pixmap("button_left"));
removeButton->setAutoDefault(true);
removeButton->setDefault(false);
gridLayout->addWidget(removeButton, 2, 1, 1, 1);
upButton = new QPushButton(this);
upButton->setObjectName(QLatin1String("upButton"));
upButton->setMinimumSize(QSize(30, 30));
upButton->setIcon(BitmapFactory().pixmap("button_up"));
gridLayout->addWidget(upButton, 3, 1, 1, 1);
downButton = new QPushButton(this);
downButton->setObjectName(QLatin1String("downButton"));
downButton->setMinimumSize(QSize(30, 30));
downButton->setIcon(BitmapFactory().pixmap("button_down"));
downButton->setAutoDefault(true);
gridLayout->addWidget(downButton, 4, 1, 1, 1);
vboxLayout = new QVBoxLayout();
vboxLayout->setContentsMargins(0, 0, 0, 0);
labelAvailable = new QLabel(this);
vboxLayout->addWidget(labelAvailable);
availableWidget = new QTreeWidget(this);
availableWidget->setObjectName(QLatin1String("availableTreeWidget"));
availableWidget->setRootIsDecorated(false);
availableWidget->setHeaderLabels(QStringList() << QString());
availableWidget->header()->hide();
vboxLayout->addWidget(availableWidget);
gridLayout->addLayout(vboxLayout, 0, 0, 6, 1);
vboxLayout1 = new QVBoxLayout();
vboxLayout1->setContentsMargins(0, 0, 0, 0);
labelSelected = new QLabel(this);
vboxLayout1->addWidget(labelSelected);
selectedWidget = new QTreeWidget(this);
selectedWidget->setObjectName(QLatin1String("selectedTreeWidget"));
selectedWidget->setRootIsDecorated(false);
selectedWidget->setHeaderLabels(QStringList() << QString());
selectedWidget->header()->hide();
vboxLayout1->addWidget(selectedWidget);
gridLayout->addLayout(vboxLayout1, 0, 2, 6, 1);
addButton->setText(QString());
removeButton->setText(QString());
upButton->setText(QString());
downButton->setText(QString());
connect(addButton, &QPushButton::clicked, this, &ActionSelector::onAddButtonClicked);
connect(removeButton, &QPushButton::clicked, this, &ActionSelector::onRemoveButtonClicked);
connect(upButton, &QPushButton::clicked, this, &ActionSelector::onUpButtonClicked);
connect(downButton, &QPushButton::clicked, this, &ActionSelector::onDownButtonClicked);
connect(availableWidget, &QTreeWidget::itemDoubleClicked, this, &ActionSelector::onItemDoubleClicked);
connect(availableWidget, &QTreeWidget::currentItemChanged, this, &ActionSelector::onCurrentItemChanged);
connect(selectedWidget, &QTreeWidget::itemDoubleClicked, this, &ActionSelector::onItemDoubleClicked);
connect(selectedWidget, &QTreeWidget::currentItemChanged, this, &ActionSelector::onCurrentItemChanged);
retranslateUi();
setButtonsEnabled();
}
ActionSelector::~ActionSelector()
{
}
void ActionSelector::setSelectedLabel(const QString& label)
{
labelSelected->setText(label);
}
QString ActionSelector::selectedLabel() const
{
return labelSelected->text();
}
void ActionSelector::setAvailableLabel(const QString& label)
{
labelAvailable->setText(label);
}
QString ActionSelector::availableLabel() const
{
return labelAvailable->text();
}
void ActionSelector::retranslateUi()
{
labelAvailable->setText(QApplication::translate("Gui::ActionSelector", "Available:"));
labelSelected->setText(QApplication::translate("Gui::ActionSelector", "Selected:"));
addButton->setToolTip(QApplication::translate("Gui::ActionSelector", "Add"));
removeButton->setToolTip(QApplication::translate("Gui::ActionSelector", "Remove"));
upButton->setToolTip(QApplication::translate("Gui::ActionSelector", "Move up"));
downButton->setToolTip(QApplication::translate("Gui::ActionSelector", "Move down"));
}
void ActionSelector::changeEvent(QEvent* event)
{
if (event->type() == QEvent::LanguageChange) {
retranslateUi();
}
QWidget::changeEvent(event);
}
void ActionSelector::keyPressEvent(QKeyEvent* event)
{
if ((event->modifiers() & Qt::ControlModifier)) {
switch (event->key())
{
case Qt::Key_Right:
onAddButtonClicked();
break;
case Qt::Key_Left:
onRemoveButtonClicked();
break;
case Qt::Key_Up:
onUpButtonClicked();
break;
case Qt::Key_Down:
onDownButtonClicked();
break;
default:
event->ignore();
return;
}
}
}
void ActionSelector::setButtonsEnabled()
{
addButton->setEnabled(availableWidget->indexOfTopLevelItem(availableWidget->currentItem()) > -1);
removeButton->setEnabled(selectedWidget->indexOfTopLevelItem(selectedWidget->currentItem()) > -1);
upButton->setEnabled(selectedWidget->indexOfTopLevelItem(selectedWidget->currentItem()) > 0);
downButton->setEnabled(selectedWidget->indexOfTopLevelItem(selectedWidget->currentItem()) > -1 &&
selectedWidget->indexOfTopLevelItem(selectedWidget->currentItem()) < selectedWidget->topLevelItemCount() - 1);
}
void ActionSelector::onCurrentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)
{
setButtonsEnabled();
}
void ActionSelector::onItemDoubleClicked(QTreeWidgetItem * item, int column)
{
Q_UNUSED(column);
QTreeWidget* treeWidget = item->treeWidget();
if (treeWidget == availableWidget) {
int index = availableWidget->indexOfTopLevelItem(item);
item = availableWidget->takeTopLevelItem(index);
availableWidget->setCurrentItem(nullptr);
selectedWidget->addTopLevelItem(item);
selectedWidget->setCurrentItem(item);
}
else if (treeWidget == selectedWidget) {
int index = selectedWidget->indexOfTopLevelItem(item);
item = selectedWidget->takeTopLevelItem(index);
selectedWidget->setCurrentItem(nullptr);
availableWidget->addTopLevelItem(item);
availableWidget->setCurrentItem(item);
}
}
void ActionSelector::onAddButtonClicked()
{
QTreeWidgetItem* item = availableWidget->currentItem();
if (item) {
int index = availableWidget->indexOfTopLevelItem(item);
item = availableWidget->takeTopLevelItem(index);
availableWidget->setCurrentItem(nullptr);
selectedWidget->addTopLevelItem(item);
selectedWidget->setCurrentItem(item);
}
}
void ActionSelector::onRemoveButtonClicked()
{
QTreeWidgetItem* item = selectedWidget->currentItem();
if (item) {
int index = selectedWidget->indexOfTopLevelItem(item);
item = selectedWidget->takeTopLevelItem(index);
selectedWidget->setCurrentItem(nullptr);
availableWidget->addTopLevelItem(item);
availableWidget->setCurrentItem(item);
}
}
void ActionSelector::onUpButtonClicked()
{
QTreeWidgetItem* item = selectedWidget->currentItem();
if (item && item->isSelected()) {
int index = selectedWidget->indexOfTopLevelItem(item);
if (index > 0) {
selectedWidget->takeTopLevelItem(index);
selectedWidget->insertTopLevelItem(index-1, item);
selectedWidget->setCurrentItem(item);
}
}
}
void ActionSelector::onDownButtonClicked()
{
QTreeWidgetItem* item = selectedWidget->currentItem();
if (item && item->isSelected()) {
int index = selectedWidget->indexOfTopLevelItem(item);
if (index < selectedWidget->topLevelItemCount()-1) {
selectedWidget->takeTopLevelItem(index);
selectedWidget->insertTopLevelItem(index+1, item);
selectedWidget->setCurrentItem(item);
}
}
}
// ------------------------------------------------------------------------------
/* TRANSLATOR Gui::AccelLineEdit */
/**
* Constructs a line edit with no text.
* The \a parent argument is sent to the QLineEdit constructor.
*/
AccelLineEdit::AccelLineEdit ( QWidget * parent )
: QLineEdit(parent)
{
setPlaceholderText(tr("Press a keyboard shortcut"));
setClearButtonEnabled(true);
keyPressedCount = 0;
}
bool AccelLineEdit::isNone() const
{
return text().isEmpty();
}
/**
* Checks which keys are pressed and show it as text.
*/
void AccelLineEdit::keyPressEvent (QKeyEvent * e)
{
QString txtLine = text();
int key = e->key();
Qt::KeyboardModifiers state = e->modifiers();
// Backspace clears the shortcut if text is present, else sets Backspace as shortcut.
// If a modifier is pressed without any other key, return.
// AltGr is not a modifier but doesn't have a QString representation.
switch(key) {
case Qt::Key_Backspace:
case Qt::Key_Delete:
if (state == Qt::NoModifier) {
keyPressedCount = 0;
if (isNone()) {
QKeySequence ks(key);
setText(ks.toString(QKeySequence::NativeText));
}
else {
clear();
}
}
case Qt::Key_Control:
case Qt::Key_Shift:
case Qt::Key_Alt:
case Qt::Key_Meta:
case Qt::Key_AltGr:
return;
default:
break;
}
// 4 keys are allowed for QShortcut
switch (keyPressedCount) {
case 4:
keyPressedCount = 0;
txtLine.clear();
break;
case 0:
txtLine.clear();
break;
default:
txtLine += QChar::fromLatin1(',');
break;
}
// Handles modifiers applying a mask.
if ((state & Qt::ControlModifier) == Qt::ControlModifier) {
QKeySequence ks(Qt::CTRL);
txtLine += ks.toString(QKeySequence::NativeText);
}
if ((state & Qt::AltModifier) == Qt::AltModifier) {
QKeySequence ks(Qt::ALT);
txtLine += ks.toString(QKeySequence::NativeText);
}
if ((state & Qt::ShiftModifier) == Qt::ShiftModifier) {
QKeySequence ks(Qt::SHIFT);
txtLine += ks.toString(QKeySequence::NativeText);
}
if ((state & Qt::MetaModifier) == Qt::MetaModifier) {
QKeySequence ks(Qt::META);
txtLine += ks.toString(QKeySequence::NativeText);
}
// Handles normal keys
QKeySequence ks(key);
txtLine += ks.toString(QKeySequence::NativeText);
setText(txtLine);
keyPressedCount++;
}
// ------------------------------------------------------------------------------
/* TRANSLATOR Gui::ModifierLineEdit */
/**
* Constructs a line edit with no text.
* The \a parent argument is sent to the QLineEdit constructor.
*/
ModifierLineEdit::ModifierLineEdit (QWidget * parent )
: QLineEdit(parent)
{
setPlaceholderText(tr("Press modifier keys"));
}
/**
* Checks which modifiers are pressed and show it as text.
*/
void ModifierLineEdit::keyPressEvent (QKeyEvent * e)
{
int key = e->key();
Qt::KeyboardModifiers state = e->modifiers();
switch (key) {
case Qt::Key_Backspace:
case Qt::Key_Delete:
clear();
return;
case Qt::Key_Control:
case Qt::Key_Shift:
case Qt::Key_Alt:
case Qt::Key_Meta:
break;
default:
return;
}
clear();
QString txtLine;
// Handles modifiers applying a mask.
if ((state & Qt::ControlModifier) == Qt::ControlModifier) {
QKeySequence ks(Qt::CTRL);
txtLine += ks.toString(QKeySequence::NativeText);
}
if ((state & Qt::AltModifier) == Qt::AltModifier) {
QKeySequence ks(Qt::ALT);
txtLine += ks.toString(QKeySequence::NativeText);
}
if ((state & Qt::ShiftModifier) == Qt::ShiftModifier) {
QKeySequence ks(Qt::SHIFT);
txtLine += ks.toString(QKeySequence::NativeText);
}
if ((state & Qt::MetaModifier) == Qt::MetaModifier) {
QKeySequence ks(Qt::META);
txtLine += ks.toString(QKeySequence::NativeText);
}
setText(txtLine);
}
// ------------------------------------------------------------------------------
ClearLineEdit::ClearLineEdit (QWidget * parent)
: QLineEdit(parent)
{
clearAction = this->addAction(QIcon(QString::fromLatin1(":/icons/edit-cleartext.svg")),
QLineEdit::TrailingPosition);
connect(clearAction, SIGNAL(triggered()), this, SLOT(clear()));
connect(this, SIGNAL(textChanged(const QString&)),
this, SLOT(updateClearButton(const QString&)));
}
void ClearLineEdit::resizeEvent(QResizeEvent *e)
{
QLineEdit::resizeEvent(e);
}
void ClearLineEdit::updateClearButton(const QString& text)
{
clearAction->setVisible(!text.isEmpty());
}
// ------------------------------------------------------------------------------
/* TRANSLATOR Gui::CheckListDialog */
/**
* Constructs a CheckListDialog which is a child of 'parent', with the
* name 'name' and widget flags set to 'f'
*
* The dialog will by default be modeless, unless you set 'modal' to
* true to construct a modal dialog.
*/
CheckListDialog::CheckListDialog( QWidget* parent, Qt::WindowFlags fl )
: QDialog( parent, fl )
, ui(new Ui_DlgTreeWidget)
{
ui->setupUi(this);
}
/**
* Destroys the object and frees any allocated resources
*/
CheckListDialog::~CheckListDialog()
{
// no need to delete child widgets, Qt does it all for us
}
/**
* Sets the items to the dialog's list view. By default all items are checkable..
*/
void CheckListDialog::setCheckableItems( const QStringList& items )
{
for (const auto & it : items) {
auto item = new QTreeWidgetItem(ui->treeWidget);
item->setText(0, it);
item->setCheckState(0, Qt::Unchecked);
}
}
/**
* Sets the items to the dialog's list view. If the boolean type of a CheckListItem
* is set to false the item is not checkable any more.
*/
void CheckListDialog::setCheckableItems( const QList<CheckListItem>& items )
{
for (const auto & it : items) {
auto item = new QTreeWidgetItem(ui->treeWidget);
item->setText(0, it.first);
item->setCheckState(0, ( it.second ? Qt::Checked : Qt::Unchecked));
}
}
/**
* Returns a list of the check items.
*/
QStringList CheckListDialog::getCheckedItems() const
{
return checked;
}
/**
* Collects all checked items to be able to return them by call \ref getCheckedItems().
*/
void CheckListDialog::accept ()
{
QTreeWidgetItemIterator it(ui->treeWidget, QTreeWidgetItemIterator::Checked);
while (*it) {
checked.push_back((*it)->text(0));
++it;
}
QDialog::accept();
}
// ------------------------------------------------------------------------------
namespace Gui {
struct ColorButtonP
{
QColor old, col;
QPointer<QColorDialog> cd;
bool allowChange;
bool autoChange;
bool drawFrame;
bool allowTransparency;
bool modal;
bool dirty;
ColorButtonP()
: cd(nullptr)
, allowChange(true)
, autoChange(false)
, drawFrame(true)
, allowTransparency(false)
, modal(true)
, dirty(true)
{
}
};
}
/**
* Constructs a colored button called \a name with parent \a parent.
*/
ColorButton::ColorButton(QWidget* parent)
: QPushButton(parent)
{
d = new ColorButtonP();
d->col = palette().color(QPalette::Active,QPalette::Midlight);
connect(this, SIGNAL(clicked()), SLOT(onChooseColor()));
int e = style()->pixelMetric(QStyle::PM_ButtonIconSize);
setIconSize(QSize(2*e, e));
}
/**
* Destroys the button.
*/
ColorButton::~ColorButton()
{
delete d;
}
/**
* Sets the color \a c to the button.
*/
void ColorButton::setColor(const QColor& c)
{
d->col = c;
d->dirty = true;
update();
}
/**
* Returns the current color of the button.
*/
QColor ColorButton::color() const
{
return d->col;
}
void ColorButton::setAllowChangeColor(bool ok)
{
d->allowChange = ok;
}
bool ColorButton::allowChangeColor() const
{
return d->allowChange;
}
void ColorButton::setDrawFrame(bool ok)
{
d->drawFrame = ok;
}
bool ColorButton::drawFrame() const
{
return d->drawFrame;
}
void Gui::ColorButton::setAllowTransparency(bool allow)
{
d->allowTransparency = allow;
if (d->cd)
d->cd->setOption(QColorDialog::ColorDialogOption::ShowAlphaChannel, allow);
}
bool Gui::ColorButton::allowTransparency() const
{
if (d->cd)
return d->cd->testOption(QColorDialog::ColorDialogOption::ShowAlphaChannel);
else
return d->allowTransparency;
}
void ColorButton::setModal(bool b)
{
d->modal = b;
}
bool ColorButton::isModal() const
{
return d->modal;
}
void ColorButton::setAutoChangeColor(bool on)
{
d->autoChange = on;
}
bool ColorButton::autoChangeColor() const
{
return d->autoChange;
}
/**
* Draws the button label.
*/
void ColorButton::paintEvent (QPaintEvent * e)
{
if (d->dirty) {
QSize isize = iconSize();
QPixmap pix(isize);
pix.fill(palette().button().color());
QPainter p(&pix);
int w = pix.width();
int h = pix.height();
p.setPen(QPen(Qt::gray));
if (d->drawFrame) {
p.setBrush(d->col);
p.drawRect(2, 2, w - 5, h - 5);
}
else {
p.fillRect(0, 0, w, h, QBrush(d->col));
}
setIcon(QIcon(pix));
d->dirty = false;
}
QPushButton::paintEvent(e);
}
void ColorButton::showModeless()
{
if (d->cd.isNull()) {
d->old = d->col;
QColorDialog* dlg = new QColorDialog(d->col, this);
dlg->setAttribute(Qt::WA_DeleteOnClose);
if (DialogOptions::dontUseNativeColorDialog())
dlg->setOptions(QColorDialog::DontUseNativeDialog);
dlg->setOption(QColorDialog::ColorDialogOption::ShowAlphaChannel, d->allowTransparency);
connect(dlg, &QColorDialog::rejected, this, &ColorButton::onRejected);
connect(dlg, &QColorDialog::currentColorChanged, this, &ColorButton::onColorChosen);
d->cd = dlg;
}
d->cd->show();
}
void ColorButton::showModal()
{
QColor currentColor = d->col;
QColorDialog* dlg = new QColorDialog(d->col, this);
dlg->setAttribute(Qt::WA_DeleteOnClose);
if (DialogOptions::dontUseNativeColorDialog())
dlg->setOptions(QColorDialog::DontUseNativeDialog);
dlg->setOption(QColorDialog::ColorDialogOption::ShowAlphaChannel, d->allowTransparency);
if (d->autoChange) {
connect(dlg, &QColorDialog::currentColorChanged, this, &ColorButton::onColorChosen);
}
dlg->setCurrentColor(currentColor);
dlg->adjustSize();
connect(dlg, &QColorDialog::finished, this, [&](int result) {
if (result == QDialog::Accepted) {
QColor c = dlg->selectedColor();
if (c.isValid()) {
setColor(c);
Q_EMIT changed();
}
}
else if (d->autoChange) {
setColor(currentColor);
Q_EMIT changed();
}
});
dlg->exec();
}
/**
* Opens a QColorDialog to set a new color.
*/
void ColorButton::onChooseColor()
{
if (!d->allowChange)
return;
if (d->modal) {
showModal();
}
else {
showModeless();
}
}
void ColorButton::onColorChosen(const QColor& c)
{
setColor(c);
Q_EMIT changed();
}
void ColorButton::onRejected()
{
setColor(d->old);
Q_EMIT changed();
}
// ------------------------------------------------------------------------------
UrlLabel::UrlLabel(QWidget* parent, Qt::WindowFlags f)
: QLabel(parent, f)
, _url (QStringLiteral("http://localhost"))
, _launchExternal(true)
{
setToolTip(this->_url);
setCursor(Qt::PointingHandCursor);
if (qApp->styleSheet().isEmpty())
setStyleSheet(QStringLiteral("Gui--UrlLabel {color: #0000FF;text-decoration: underline;}"));
}
UrlLabel::~UrlLabel()
{
}
void Gui::UrlLabel::setLaunchExternal(bool l)
{
_launchExternal = l;
}
void UrlLabel::mouseReleaseEvent(QMouseEvent*)
{
if (_launchExternal)
QDesktopServices::openUrl(this->_url);
else
// Someone else will deal with it...
Q_EMIT linkClicked(_url);
}
QString UrlLabel::url() const
{
return this->_url;
}
bool Gui::UrlLabel::launchExternal() const
{
return _launchExternal;
}
void UrlLabel::setUrl(const QString& u)
{
this->_url = u;
setToolTip(this->_url);
}
// --------------------------------------------------------------------
StatefulLabel::StatefulLabel(QWidget* parent)
: QLabel(parent)
, _overridePreference(false)
{
// Always attach to the parameter group that stores the main FreeCAD stylesheet
_stylesheetGroup = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/General");
_stylesheetGroup->Attach(this);
}
StatefulLabel::~StatefulLabel()
{
if (_parameterGroup.isValid())
_parameterGroup->Detach(this);
_stylesheetGroup->Detach(this);
}
void StatefulLabel::setDefaultStyle(const QString& defaultStyle)
{
_defaultStyle = defaultStyle;
}
void StatefulLabel::setParameterGroup(const std::string& groupName)
{
if (_parameterGroup.isValid())
_parameterGroup->Detach(this);
// Attach to the Parametergroup so we know when it changes
_parameterGroup = App::GetApplication().GetParameterGroupByPath(groupName.c_str());
if (_parameterGroup.isValid())
_parameterGroup->Attach(this);
}
void StatefulLabel::registerState(const QString& state, const QString& styleCSS,
const std::string& preferenceName)
{
_availableStates[state] = { styleCSS, preferenceName };
}
void StatefulLabel::registerState(const QString& state, const QColor& color,
const std::string& preferenceName)
{
QString css;
if (color.isValid())
css = QString::fromUtf8("Gui--StatefulLabel{ color : rgba(%1,%2,%3,%4) ;}").arg(color.red()).arg(color.green()).arg(color.blue()).arg(color.alpha());
_availableStates[state] = { css, preferenceName };
}
void StatefulLabel::registerState(const QString& state, const QColor& fg, const QColor& bg,
const std::string& preferenceName)
{
QString colorEntries;
if (fg.isValid())
colorEntries.append(QString::fromUtf8("color : rgba(%1,%2,%3,%4);").arg(fg.red()).arg(fg.green()).arg(fg.blue()).arg(fg.alpha()));
if (bg.isValid())
colorEntries.append(QString::fromUtf8("background-color : rgba(%1,%2,%3,%4);").arg(bg.red()).arg(bg.green()).arg(bg.blue()).arg(bg.alpha()));
QString css = QString::fromUtf8("Gui--StatefulLabel{ %1 }").arg(colorEntries);
_availableStates[state] = { css, preferenceName };
}
/** Observes the parameter group and clears the cache if it changes */
void StatefulLabel::OnChange(Base::Subject<const char*>& rCaller, const char* rcReason)
{
Q_UNUSED(rCaller);
auto changedItem = std::string(rcReason);
if (changedItem == "StyleSheet") {
_styleCache.clear();
}
else {
for (const auto& state : _availableStates) {
if (state.second.preferenceString == changedItem) {
_styleCache.erase(_styleCache.find(state.first));
}
}
}
}
void StatefulLabel::setOverridePreference(bool overridePreference)
{
_overridePreference = overridePreference;
}
void StatefulLabel::setState(QString state)
{
_state = state;
this->ensurePolished();
// If the stylesheet insists, ignore all other logic and let it do its thing. This
// property is *only* set by the stylesheet.
if (_overridePreference)
return;
// Check the cache first:
if (auto style = _styleCache.find(_state); style != _styleCache.end()) {
auto test = style->second.toStdString();
this->setStyleSheet(style->second);
return;
}
if (auto entry = _availableStates.find(state); entry != _availableStates.end()) {
// Order of precedence: first, check if the user has set this in their preferences:
if (!entry->second.preferenceString.empty()) {
// First, try to see if it's just stored a color (as an unsigned int):
auto availableColorPrefs = _parameterGroup->GetUnsignedMap();
std::string lookingForGroup = entry->second.preferenceString;
for (const auto &unsignedEntry : availableColorPrefs) {
std::string foundGroup = unsignedEntry.first;
if (unsignedEntry.first == entry->second.preferenceString) {
// Convert the stored Uint into usable color data:
unsigned int col = unsignedEntry.second;
QColor qcolor((col >> 24) & 0xff, (col >> 16) & 0xff, (col >> 8) & 0xff);
this->setStyleSheet(QString::fromUtf8("Gui--StatefulLabel{ color : rgba(%1,%2,%3,%4) ;}").arg(qcolor.red()).arg(qcolor.green()).arg(qcolor.blue()).arg(qcolor.alpha()));
_styleCache[state] = this->styleSheet();