-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWizEditorToolBar.cpp
More file actions
2844 lines (2466 loc) · 99.5 KB
/
WizEditorToolBar.cpp
File metadata and controls
2844 lines (2466 loc) · 99.5 KB
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
#include "WizEditorToolBar.h"
#include <QStylePainter>
#include <QToolButton>
#include <QFontComboBox>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QDesktopServices>
#include <QAction>
#include <QMenu>
#include <QFileDialog>
#include <QImageWriter>
#include <QClipboard>
#include <QApplication>
#include <QMimeData>
#include <QStyledItemDelegate>
#include <QListWidget>
#include <QFontDialog>
#include <QDebug>
#include <QWidgetAction>
#include <QActionGroup>
#include <QPixmap>
#include "share/WizMisc.h"
#include "WizDef.h"
#include "share/WizSettings.h"
#include "WizDocumentWebEngine.h"
#include "WizDocumentWebView.h"
#include "WizActions.h"
#include "utils/WizLogger.h"
#include "share/WizObjectDataDownloader.h"
#include "share/WizAnalyzer.h"
#include "WizDef.h"
#include "utils/WizStyleHelper.h"
#include "WizDocumentView.h"
#include "widgets/WizTipsWidget.h"
#include "WizMainWindow.h"
#include "widgets/WizTableSelector.h"
#include "share/jsoncpp/json/json.h"
const int WizCheckStateRole = (int)Qt::UserRole + 5;
const int WizFontFamilyHelperRole = WizCheckStateRole + 1;
#define RecommendedWidthForTwoLine WizSmartScaleUI(685)
#define RecommendedWidthForOneLine WizSmartScaleUI(1060)
#define WIZSEPARATOR "separator"
#define WIZFONTPANEL "fontpanel"
#define WIZRECENTFONT "recentfont"
#define WIZRECENTFONTLIST "RecentlyUsedFont"
#define WIZSHOWEXTRABUTTONITEMS "ShowExtraButtonItems"
struct WizComboboxStyledItem
{
QString strText;
QString strUserData;
QString strFontFamily;
int nFontSize;
bool bBold;
};
const int nCommonlyUsedFontCount = 9;
const QString CommonlyUsedFont[] =
{
"Arial",
"Helvetica Neue",
"Times",
"Times New Roman",
"Tahoma",
"Verdana",
"Songti SC",
"Heiti SC",
"Kaiti SC"
};
const int nParagraphItemCount = 8;
WizComboboxStyledItem* ParagraphItems()
{
static WizComboboxStyledItem paragraphItems[] =
{
{QObject::tr("Paragraph"), "p", "", 14, false},
{QObject::tr("Text"), "div", "", 14, false},
{QObject::tr("H6"), "h6", "", 14, true},
{QObject::tr("H5"), "h5", "", 15, true},
{QObject::tr("H4"), "h4", "", 17, true},
{QObject::tr("H3"), "h3", "", 18, true},
{QObject::tr("H2"), "h2", "", 20, true},
{QObject::tr("H1"), "h1", "", 21, true }
};
return paragraphItems;
}
const int nFontSizeCount = 15;
WizComboboxStyledItem* FontSizes()
{
static WizComboboxStyledItem fontItems[] =
{
{"9", "9pt", "", 14, false},
{"10", "10pt", "", 14, false},
{"11", "11pt", "", 14, false},
{"12", "12pt", "", 14, false},
{"13", "13pt", "", 14, false},
{"14", "14pt", "", 14, false},
{"15", "15pt", "", 14, false},
{"16", "16pt", "", 14, false},
{"17", "17pt", "", 14, false},
{"18", "18pt", "", 14, false},
{"24", "24pt", "", 14, false},
{"36", "36pt", "", 14, false},
{"48", "48pt", "", 14, false},
{"64", "64pt", "", 14, false},
{"72", "72pt", "", 14, false}
};
return fontItems;
}
const int nFontFamilyCount = 30;
WizComboboxStyledItem* FontFamilies()
{
static WizComboboxStyledItem fontItems[] =
{
{QObject::tr("Adobe Fangsong Std"), "Adobe Fangsong Std", "Adobe Fangsong Std", 14, false},
{QObject::tr("Adobe Heiti Std"), "Adobe Heiti Std", "Adobe Heiti Std", 14, false},
{QObject::tr("Adobe Kaiti Std"), "Adobe Kaiti Std", "Adobe Kaiti Std", 14, false},
{QObject::tr("Adobe Song Std"), "Adobe Song Std", "Adobe Song Std", 14, false},
{QObject::tr("Baoli SC"), "Baoli SC", "Baoli SC", 14, false},
{QObject::tr("Hannotate SC"), "Hannotate SC", "Hannotate SC", 14, false},
{QObject::tr("Hannotate TC"), "Hannotate TC", "Hannotate TC", 14, false},
{QObject::tr("HanziPen SC"), "HanziPen SC", "HanziPen SC", 14, false},
{QObject::tr("HanziPen TC"), "HanziPen TC", "HanziPen TC", 14, false},
{QObject::tr("Heiti SC"), "Heiti SC", "Heiti SC", 14, false},
{QObject::tr("Heiti TC"), "Heiti TC", "Heiti TC", 14, false},
{QObject::tr("Kaiti SC"), "Kaiti SC", "Kaiti SC", 14, false},
{QObject::tr("Kaiti TC"), "Kaiti TC", "Kaiti TC", 14, false},
{QObject::tr("Lantinghei SC"), "Lantinghei SC", "Lantinghei SC", 14, false},
{QObject::tr("Lantinghei TC"), "Lantinghei TC", "Lantinghei TC", 14, false},
{QObject::tr("Libian SC"), "Libian SC", "Libian SC", 14, false},
{QObject::tr("Microsoft YaHei"), "Microsoft YaHei", "Microsoft YaHei", 14, false},
{QObject::tr("Songti SC"), "Songti SC", "Songti SC", 14, false},
{QObject::tr("Songti TC"), "Songti TC", "Songti TC", 14, false},
{QObject::tr("STFangsong"), "STFangsong", "STFangsong", 14, false},
{QObject::tr("STHeiti"), "STHeiti", "STHeiti", 14, false},
{QObject::tr("STKaiti"), "STKaiti", "STKaiti", 14, false},
{QObject::tr("STSong"), "STSong", "STSong", 14, false},
{QObject::tr("Wawati SC"), "Wawati SC", "Wawati SC", 14, false},
{QObject::tr("Wawati TC"), "Wawati TC", "Wawati TC", 14, false},
{QObject::tr("Weibei SC"), "Weibei SC", "Weibei SC", 14, false},
{QObject::tr("Weibei TC"), "Weibei TC", "Weibei TC", 14, false},
{QObject::tr("Xingkai SC"), "Xingkai SC", "Xingkai SC", 14, false},
{QObject::tr("Yuanti SC"), "Yuanti SC", "Yuanti SC", 14, false},
{QObject::tr("Yuppy TC"), "Yuppy TC", "Yuppy TC", 14, false},
{QObject::tr("Yuppy SC"), "Yuppy SC", "Yuppy SC", 14, false}
};
return fontItems;
};
WizComboboxStyledItem itemFromArrayByKey(const QString& key, const WizComboboxStyledItem array[], const int count)
{
for (int i = 0; i < count; i++)
{
if (array[i].strUserData == key)
{
return array[i];
}
}
WizComboboxStyledItem defaultItem;
return defaultItem;
}
WizComboboxStyledItem itemFromArrayByText(const QString& text, const WizComboboxStyledItem array[], const int count)
{
for (int i = 0; i < count; i++)
{
if (array[i].strText == text)
{
return array[i];
}
}
WizComboboxStyledItem defaultItem;
return defaultItem;
}
void clearWizCheckState(QComboBox* combobox)
{
for (int i = 0; i < combobox->count(); i++)
{
QModelIndex index = combobox->model()->index(i, 0);
combobox->model()->setData(index, Qt::Unchecked, WizCheckStateRole);
}
}
class WizToolComboboxItemDelegate : public QStyledItemDelegate
{
public:
WizToolComboboxItemDelegate(QObject *parent, QComboBox* widget, const WizComboboxStyledItem* items, int count)
: QStyledItemDelegate(parent), m_itemArray(items), m_arrayCount(count), m_widget(widget)
{}
void drawTextItem(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index, bool useDefaultFont = false) const
{
QStyleOptionViewItem opt = option;
initStyleOption(&opt, index);
//
if (option.state & QStyle::State_Selected)
{
painter->fillRect(option.rect, QBrush(QColor("#5990EF")));
opt.palette.setColor(QPalette::Text, QColor(Qt::white));
opt.palette.setColor(QPalette::WindowText, QColor(Qt::white));
opt.palette.setColor(QPalette::HighlightedText, QColor(Qt::white));
}
else if (option.state & QStyle::State_MouseOver)
{
opt.palette.setColor(QPalette::Text, QColor(Qt::black));
opt.palette.setColor(QPalette::WindowText, QColor(Qt::black));
opt.palette.setColor(QPalette::HighlightedText, QColor(Qt::black));
}
//
if (!useDefaultFont)
{
QString text = index.model()->data(index, Qt::DisplayRole).toString();
WizComboboxStyledItem styledItem = itemFromArrayByText(text, m_itemArray, m_arrayCount);
if (styledItem.strText.isEmpty())
{
//set default data
styledItem.bBold = false;
styledItem.nFontSize = 14;
styledItem.strFontFamily = text;
}
opt.font.setPointSize(styledItem.nFontSize);
opt.font.setBold(styledItem.bBold);
if (!styledItem.strFontFamily.isEmpty())
{
opt.font.setFamily(styledItem.strFontFamily);
}
}
const int nIconSize = WizSmartScaleUI(16);
if (index.model()->data(index, WizCheckStateRole).toInt() == Qt::Checked)
{
static QIcon icon = Utils::WizStyleHelper::loadIcon("listViewItemSelected");
QPixmap pix = icon.pixmap(QSize(nIconSize, nIconSize), QIcon::Normal, (opt.state & QStyle::State_MouseOver) ? QIcon::On : QIcon::Off);
if (!pix.isNull())
{
painter->drawPixmap(opt.rect.x() + 4, opt.rect.y() + (opt.rect.height() - nIconSize) / 2, nIconSize, nIconSize , pix);
}
}
opt.rect.setX(opt.rect.x() + nIconSize + 4);
QStyledItemDelegate::paint(painter, opt, index);
}
void drawSeparatorItem(QPainter *painter,
const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem opt = option;
initStyleOption(&opt, index);
//
painter->save();
painter->setPen(QColor("#cccccc"));
painter->setBrush(Qt::NoBrush);
int lineY = opt.rect.y() + opt.rect.height() / 2;
painter->drawLine(QLine(opt.rect.x(), lineY, opt.rect.right(), lineY));
painter->restore();
}
void paint(QPainter *painter,
const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QString helperData = index.data(WizFontFamilyHelperRole).toString();
if (helperData.isEmpty() || helperData == WIZRECENTFONT)
{
drawTextItem(painter, option, index);
}
else if (helperData == WIZSEPARATOR)
{
drawSeparatorItem(painter, option, index);
}
else
{
drawTextItem(painter, option, index, true);
}
}
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem opt = option;
initStyleOption(&opt, index);
if (index.data(WizFontFamilyHelperRole).toString() == WIZSEPARATOR)
{
return QSize(opt.rect.width(), 5);
}
//
WizComboboxStyledItem styledItem = itemFromArrayByText(opt.text, m_itemArray, m_arrayCount);
if (styledItem.strText.isEmpty())
{
styledItem.nFontSize = WizSmartScaleUI(14);
}
QFont font = opt.font;
font.setPointSize(styledItem.nFontSize);
QFontMetrics fm(font);
QRect rc = fm.boundingRect(opt.text);
//
QSize size(rc.width() + 4, rc.height() + 4);
return size;
}
private:
const WizComboboxStyledItem* m_itemArray;
int m_arrayCount;
QComboBox* m_widget;
};
const QColor colors[6][8] =
{
{QColor(0, 0, 0, 255), QColor(170, 0, 0, 255), QColor(0, 85, 0, 255), QColor(170, 85, 0, 255),
QColor(0, 170, 0, 255), QColor(170, 170, 0, 255), QColor(0, 255, 0, 255), QColor(170, 250, 0, 255)},
{QColor(0, 0, 127, 255), QColor(170, 0, 127, 255), QColor(0, 85, 127, 255), QColor(170, 85, 127, 255),
QColor(0, 170, 127, 255), QColor(170, 170, 127, 255), QColor(0, 255, 127, 255), QColor(170, 255, 127, 255)},
{QColor(0, 0, 255, 255), QColor(170, 0, 255, 255), QColor(0, 85, 255, 255), QColor(170, 85, 255, 255),
QColor(0, 170, 255, 255), QColor(170, 170, 255, 255), QColor(0, 255, 255, 255), QColor(170, 255, 255, 255)},
{QColor(85, 0, 0, 255), QColor(255, 0, 0, 255), QColor(85, 85, 0, 255), QColor(255, 85, 0, 255),
QColor(85, 170, 0, 255), QColor(255, 170, 0, 255), QColor(85, 255, 0, 255), QColor(255, 255, 0, 255)},
{QColor(85, 0, 127, 255), QColor(255, 0, 127, 255), QColor(85, 85, 127, 255), QColor(255, 85, 127, 255),
QColor(85, 170, 127, 255), QColor(255, 170, 127, 255), QColor(85, 255, 127, 255), QColor(255, 255, 127, 255)},
{QColor(85, 0, 255, 255), QColor(255, 0, 255, 255), QColor(85, 85, 255, 255), QColor(255, 85, 255, 255),
QColor(85, 170, 255, 255), QColor(255, 170, 255, 255), QColor(85, 255, 255, 255), QColor(255, 255, 255, 255)}
};
const int nMacColorRow = 6;
const int nMacColorColum = 6;
const QColor macColors[nMacColorRow][nMacColorColum] =
{
{QColor("#00b0f0"), QColor("#92d050"), QColor("#ffff00"),
QColor("#fcc00f"), QColor("#ff0000"), QColor("#7030a0")},
{QColor(100, 179, 223, 255), QColor(157, 225, 89, 255), QColor(255, 224, 97, 255),
QColor(255, 192, 114, 255), QColor(255, 95, 94, 255), QColor(157, 69, 184, 255)},
{QColor(73, 155, 201, 255), QColor(111, 192, 55, 255), QColor(241, 209, 48, 255),
QColor(255, 169, 58, 255), QColor(255, 44, 33, 255), QColor(107, 33, 133, 255)},
{QColor(54, 125, 162, 255), QColor(122, 174, 61, 255), QColor(226, 184, 0, 255),
QColor(236, 159, 46, 255), QColor(206, 34, 43, 255), QColor(84, 20, 108, 255)},
{QColor(23, 87, 121, 255), QColor(88, 135, 37, 255), QColor(198, 147, 0, 255),
QColor(209, 127, 20, 255), QColor(174, 25, 22, 255), QColor(60, 10, 74, 255)},
{QColor(255, 255, 255, 255), QColor(204, 204, 204, 255), QColor(153, 153, 153, 255),
QColor(102, 102, 102, 255), QColor(51, 51, 51, 255), QColor(0, 0, 0, 255)},
};
QIcon createColorIcon(QColor color)
{
QPixmap pixmap(WizSmartScaleUI(16), WizSmartScaleUI(16));
QPainter painter(&pixmap);
painter.setPen(Qt::NoPen);
painter.fillRect(QRect(0, 0, WizSmartScaleUI(16), WizSmartScaleUI(16)), color);
return QIcon(pixmap);
}
#define TOOLBUTTON_MARGIN_WIDTH WizSmartScaleUI(12)
#define TOOLBUTTON_ARRWO_WIDTH WizSmartScaleUI(16)
void drawComboPrimitive(QStylePainter* p, QStyle::PrimitiveElement pe, const QStyleOption &opt);
void drawButtonBackground(QPainter* painter, const QRect& rect, bool bDrawLeft, bool bDrawRight, bool hasFocus)
{
int nScale = WizIsHighPixel() ? 2 : 1;
// load file
static QPixmap normalPixBackground = QPixmap(Utils::WizStyleHelper::skinResourceFileName("editorToolButtonBackground", true));
static QPixmap focusPixBackground = QPixmap(Utils::WizStyleHelper::skinResourceFileName("editorToolButtonBackground_on", true));
static QPixmap normalPixBackgroundMid = normalPixBackground.copy(6, 0, 2, normalPixBackground.height());
static QPixmap focusPixBackgroundMid = focusPixBackground.copy(6, 0, 2, focusPixBackground.height());
QRect rcLeft(rect.x(), rect.y(), 6, rect.size().height());
static QPixmap normalPixBackgroundLeft = normalPixBackground.copy(0, 0, 6 * nScale, normalPixBackground.height());
static QPixmap focusPixBackgroundLeft = focusPixBackground.copy(0, 0, 6 * nScale, focusPixBackground.height());
int rightWidth = 8;
static QPixmap normalPixBackgroundRight = normalPixBackground.copy(normalPixBackground.width() - rightWidth * nScale, 0, rightWidth * nScale, normalPixBackground.height());
static QPixmap focusPixBackgroundRight = focusPixBackground.copy(focusPixBackground.width() - rightWidth * nScale, 0, rightWidth * nScale, focusPixBackground.height());
QRect rcRight(rect.size().width() - rightWidth, rect.y(), rightWidth, rect.size().height());
const QPixmap& pixLeft = hasFocus ? focusPixBackgroundLeft : normalPixBackgroundLeft;
const QPixmap& pixMid = hasFocus ? focusPixBackgroundMid : normalPixBackgroundMid;
const QPixmap& pixRight = hasFocus ? focusPixBackgroundRight : normalPixBackgroundRight;
//
painter->drawPixmap(rcLeft, bDrawLeft ? pixLeft : pixMid);
painter->drawPixmap(rect.x() + 5, rect.y(), rect.size().width() - rightWidth, rect.size().height(), pixMid);
painter->drawPixmap(rcRight, bDrawRight ? pixRight : pixMid);
}
void drawCombo(QComboBox* cm, QStyleOptionComboBox& opt)
{
QStylePainter painter(cm);
opt.palette.setColor(QPalette::Text, "#646464");
painter.setPen(cm->palette().color(QPalette::Text));
drawButtonBackground(&painter, opt.rect, true, true, opt.state & QStyle::State_MouseOver);
// draw arrow
if (opt.subControls & QStyle::SC_ComboBoxArrow) {
QStyleOption subOpt = opt;
QRect rectSub = cm->style()->subControlRect(QStyle::CC_ComboBox, &opt, QStyle::SC_ComboBoxArrow);
rectSub.adjust(6, 0, -12, 0);
subOpt.rect = rectSub.adjusted(0, rectSub.height()/2 - 3, 0, -rectSub.height()/2 + 3);
QRect rcArrow = opt.rect;
rcArrow.setX(opt.rect.right() - TOOLBUTTON_ARRWO_WIDTH - 8);
rcArrow.setY((opt.rect.height() - TOOLBUTTON_ARRWO_WIDTH) / 2);
rcArrow.setSize(QSize(TOOLBUTTON_ARRWO_WIDTH, TOOLBUTTON_ARRWO_WIDTH));
static QPixmap arrow = QPixmap(Utils::WizStyleHelper::skinResourceFileName("editorToolbarComboboxArrow", true));
painter.drawPixmap(rcArrow, arrow);
}
// draw text
QFont f = painter.font();
f.setPixelSize(Utils::WizStyleHelper::editComboFontSize());
painter.setFont(f);
QRect editRect = cm->style()->subControlRect(QStyle::CC_ComboBox, &opt, QStyle::SC_ComboBoxEditField);
if (!opt.currentText.isEmpty()) {
painter.drawItemText(editRect.adjusted(1, 0, -1, 0),
cm->style()->visualAlignment(opt.direction, Qt::AlignLeft | Qt::AlignVCenter),
opt.palette, opt.state & QStyle::State_Enabled, opt.currentText, QPalette::Text);
}
}
void drawComboPrimitive(QStylePainter* p, QStyle::PrimitiveElement pe, const QStyleOption &opt)
{
p->save();
int xOffset = opt.direction == Qt::LeftToRight ? 2 : -1;
QMatrix matrix;
matrix.translate(opt.rect.center().x() + xOffset, opt.rect.center().y() + 2);
QPainterPath path;
switch(pe) {
default:
case QStyle::PE_IndicatorArrowDown:
break;
case QStyle::PE_IndicatorArrowUp:
matrix.rotate(180);
break;
case QStyle::PE_IndicatorArrowLeft:
matrix.rotate(90);
break;
case QStyle::PE_IndicatorArrowRight:
matrix.rotate(-90);
break;
}
path.moveTo(0, 2.3);
path.lineTo(-2.3, -2.3);
path.lineTo(2.3, -2.3);
p->setMatrix(matrix);
p->setPen(Qt::NoPen);
p->setBrush(QColor(0, 0, 0, 255));
p->setRenderHint(QPainter::Antialiasing);
p->drawPath(path);
p->restore();
}
WizDblclickableToolButton::WizDblclickableToolButton(QWidget *parent)
: QToolButton(parent)
{
m_tDblClicked = QDateTime::currentDateTime();
}
void WizDblclickableToolButton::mouseDoubleClickEvent(QMouseEvent *event)
{
QToolButton::mouseDoubleClickEvent(event);
m_tDblClicked = QDateTime::currentDateTime();
//
emit dblClicked();
}
void WizDblclickableToolButton::mouseReleaseEvent(QMouseEvent* event)
{
QDateTime now = QDateTime::currentDateTime();
int seconds = now.toTime_t() - m_tDblClicked.toTime_t();
if (seconds < 1) {
event->ignore();
} else {
QToolButton::mouseReleaseEvent(event);
}
}
class CWizToolButton : public WizDblclickableToolButton
{
public:
enum Position {
NoPosition,
left,
Center,
right
};
CWizToolButton(QWidget* parent = 0)
: WizDblclickableToolButton(parent)
, m_colorHoverBorder("#c8dae8")
, m_colorHoverFill("#e8f0f3")
, m_colorSunkenBorder("#0072c4")
, m_horizontalPadding_left(WizSmartScaleUI(10))
, m_horizontalPadding_rgiht(WizSmartScaleUI(10))
{
setFocusPolicy(Qt::NoFocus);
setCheckable(true);
setIconSize(QSize(Utils::WizStyleHelper::editIconHeight(), Utils::WizStyleHelper::editIconHeight()));
setFixedHeight(Utils::WizStyleHelper::editorButtonHeight());
}
void setPosition(Position position)
{
m_position = position;
}
void setHorizontalPadding(int padding)
{
m_horizontalPadding_left = padding;
m_horizontalPadding_rgiht = padding;
}
void setHorizontalPadding(int leftPadding, int rightPadding)
{
m_horizontalPadding_left = leftPadding;
m_horizontalPadding_rgiht = rightPadding;
}
protected:
virtual void leaveEvent(QEvent* event)
{
WizDblclickableToolButton::leaveEvent(event);
update();
}
virtual void enterEvent(QEvent* event)
{
WizDblclickableToolButton::enterEvent(event);
update();
}
void mouseReleaseEvent(QMouseEvent* ev)
{
#ifdef Q_OS_OSX
QMenu* m = menu();
if (m)
{
QPoint pt = mapToGlobal(rect().bottomLeft());
m->popup(pt);
return;
}
#endif
//
WizDblclickableToolButton::mouseReleaseEvent(ev);
}
virtual void paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QStyleOptionToolButton opt;
initStyleOption(&opt);
QPainter p(this);
p.setClipRect(opt.rect);
QIcon::Mode mode = opt.state & QStyle::State_Enabled ? QIcon::Normal : QIcon::Disabled;
if (mode == QIcon::Normal && (opt.state & QStyle::State_Sunken))
mode = QIcon::Active;
QIcon::State state = QIcon::Off;
if (opt.state & QStyle::State_On)
state = QIcon::On;
bool bDrawLeft = (m_position == left) || (m_position == NoPosition);
bool bDrawRight = (m_position == right) || (m_position == NoPosition);
drawButtonBackground(&p, opt.rect, bDrawLeft, bDrawRight, opt.state & QStyle::State_MouseOver);
QSize size = iconSize();
QRect rcIcon((opt.rect.width() - size.width()) / 2, (opt.rect.height() - size.height()) / 2, size.width(), size.height());
if (opt.arrowType == Qt::RightArrow)
rcIcon.setX((opt.rect.width() - size.width()) / 2 - TOOLBUTTON_MARGIN_WIDTH);
opt.icon.paint(&p, rcIcon, Qt::AlignCenter, mode, state);
if (opt.arrowType == Qt::RightArrow)
{
QRect rcArrow = opt.rect;
rcArrow.setX(opt.rect.right() - TOOLBUTTON_ARRWO_WIDTH - 3);
rcArrow.setY((opt.rect.height() - TOOLBUTTON_ARRWO_WIDTH) / 2);
rcArrow.setSize(QSize(TOOLBUTTON_ARRWO_WIDTH, TOOLBUTTON_ARRWO_WIDTH));
static QPixmap arrow = QPixmap(Utils::WizStyleHelper::skinResourceFileName("editorToolbarDownArrow", true));
p.drawPixmap(rcArrow, arrow);
}
}
QSize sizeHint() const
{
int width = m_horizontalPadding_left + m_horizontalPadding_rgiht + iconSize().width();
if (arrowType() == Qt::RightArrow)
return QSize(width + TOOLBUTTON_MARGIN_WIDTH, Utils::WizStyleHelper::editorButtonHeight());
return QSize(width, Utils::WizStyleHelper::editorButtonHeight());
}
Position m_position;
int m_horizontalPadding_left;
int m_horizontalPadding_rgiht;
private:
QColor m_colorHoverBorder;
QColor m_colorHoverFill;
QColor m_colorSunkenBorder;
};
#define ColorButtonRightArrowWidth WizSmartScaleUI(18)
class CWizToolButtonColor : public CWizToolButton
{
public:
CWizToolButtonColor(QWidget* parent = 0) : CWizToolButton(parent)
, m_menu(NULL)
, m_color(Qt::transparent)
{
setCheckable(false);
// setIconSize(QSize(Utils::WizStyleHelper::editorButtonHeight(), Utils::WizStyleHelper::editorButtonHeight()));
setPopupMode(QToolButton::MenuButtonPopup);
}
void setColor(const QColor& color)
{
m_color = color;
repaint();
}
QColor color() const
{
return m_color;
}
//修复按钮点击不弹出菜单的问题
void setMenu(QMenu* menu)
{
m_menu = menu;
QToolButton::setMenu(nullptr);
}
QMenu* menu() const
{
return m_menu;
}
protected:
virtual void paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QStyleOptionToolButton opt;
initStyleOption(&opt);
QStylePainter p(this);
p.setClipRect(opt.rect);
bool bDrawLeft = (m_position == left) || (m_position == NoPosition);
bool bDrawRight = (m_position == right) || (m_position == NoPosition);
drawButtonBackground(&p, opt.rect, bDrawLeft, bDrawRight, opt.state & QStyle::State_MouseOver);
//
QIcon::Mode mode = opt.state & QStyle::State_Enabled ? QIcon::Normal : QIcon::Disabled;
if (mode == QIcon::Normal && (opt.state & QStyle::State_HasFocus || opt.state & QStyle::State_Sunken))
mode = QIcon::Active;
QIcon::State state = QIcon::Off;
if (opt.state & QStyle::State_On)
state = QIcon::On;
QSize size = iconSize();
QRect rcIcon((opt.rect.width() - size.width() - ColorButtonRightArrowWidth) / 2, (opt.rect.height() - size.height()) / 2, size.width(), size.height());
opt.icon.paint(&p, rcIcon, Qt::AlignCenter, mode, state);
QRect rectColor(rcIcon.x() + 1, opt.iconSize.height() + 6, opt.iconSize.width() - 2, 2);
p.fillRect(QRect(rectColor), m_color);
if (opt.state & QStyle::State_MouseOver)
{
QPoint top(rcIcon.right() + WizSmartScaleUI(7), opt.rect.x() + (opt.rect.height() - 13) / 2);
p.setPen(QPen(QColor("#C4C4C4")));
p.drawLine(top, QPoint(top.x(), top.y() + 13));
}
//arrow
static QPixmap arrow = QPixmap(Utils::WizStyleHelper::skinResourceFileName("editorToolbarDownArrow", true));
#ifdef Q_OS_MAC
int arrowHeight = arrow.height() * (WizIsHighPixel() ? 0.5f : 1);
#else
int arrowHeight = TOOLBUTTON_ARRWO_WIDTH;
#endif
QRect rcArrow(rcIcon.right() + WizSmartScaleUI(7), (opt.rect.height() - arrowHeight) / 2, TOOLBUTTON_ARRWO_WIDTH, TOOLBUTTON_ARRWO_WIDTH);
p.drawPixmap(rcArrow, arrow);
}
void mousePressEvent(QMouseEvent* ev)
{
QToolButton::mousePressEvent(ev);
}
void mouseReleaseEvent(QMouseEvent* ev)
{
QPoint pos = ev->pos();
QRect rcIcon = rect();
rcIcon.setWidth(rcIcon.width() - ColorButtonRightArrowWidth);
if (rcIcon.contains(pos))
{
QToolButton::mouseReleaseEvent(ev);
return;
}
ev->ignore();
QRect rcArrow = rect();
rcArrow.setLeft(rcIcon.right());
if (m_menu && rcArrow.contains(pos))
{
QPoint pt = mapToGlobal(rect().bottomLeft());
m_menu->popup(pt);
}
}
QSize sizeHint() const
{
QSize size = CWizToolButton::sizeHint();
size.setWidth(size.width() + ColorButtonRightArrowWidth);
return size;
}
private:
QColor m_color;
QMenu* m_menu;
};
class CWizToolComboBox : public QComboBox
{
public:
CWizToolComboBox(QWidget* parent = 0)
: QComboBox(parent)
, m_isPopup(false)
{
setFocusPolicy(Qt::NoFocus);
setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength);
QFont f = font();
f.setPixelSize(Utils::WizStyleHelper::editComboFontSize());
setFont(f);
}
void setText(const QString& strText)
{
int index = findText(strText);
if (index != -1) {
setCurrentIndex(index);
}
m_strText = strText;
repaint();
}
//
void setFontName(const QString& strFontName)
{
CWizStdStringArray arrayFontName;
WizSplitTextToArray(strFontName, _T(','), arrayFontName);
//
QString firstName;
//
for (auto name : arrayFontName)
{
QString fontName = name;
fontName = fontName.trimmed();
fontName.remove("\"");
fontName.remove("\'");
//
if (firstName.isEmpty())
firstName = fontName;
//
int index = findText(fontName);
if (index != -1) {
setCurrentIndex(index);
m_strText = fontName;
return;
}
}
//
m_strText = firstName;
setCurrentIndex(-1);
}
QString text() const { return m_strText; }
bool isPopuping() const { return m_isPopup; }
void showPopup()
{
m_isPopup = true;
QComboBox::showPopup();
}
void hidePopup()
{
m_isPopup = false;
QComboBox::hidePopup();
}
bool event(QEvent* event)
{
if (event->type() == QEvent::Paint)
{
//FIXME: QT5.4.1 通过点击combobox外区域来隐藏列表不会触发 hidePopup() 事件
if (m_isPopup)
{
m_isPopup = false;
}
}
return QComboBox::event(event);
}
protected:
virtual void leaveEvent(QEvent* event) {
QComboBox::leaveEvent(event);
update();
}
virtual void enterEvent(QEvent* event) {
QComboBox::enterEvent(event);
update();
}
virtual QSize sizeHint() const
{
return QSize(65, Utils::WizStyleHelper::editorButtonHeight());
}
virtual void paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QStyleOptionComboBox opt;
initStyleOption(&opt);
opt.currentText = m_strText;
drawCombo(this, opt);
}
private:
QString m_strText;
bool m_isPopup;
};
class CWizToolComboBoxFont : public QFontComboBox
{
public:
CWizToolComboBoxFont(QWidget* parent = 0)
: QFontComboBox(parent)
, m_isPopup(false)
{
setFocusPolicy(Qt::NoFocus);
setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength);
setEditable(false);
}
void setText(const QString& strText)
{
int index = findText(strText);
if (index != -1) {
setCurrentIndex(index);
}
m_strText = strText;
repaint();
}
QString text() const { return m_strText; }
bool isPopuping() const { return m_isPopup; }
void showPopup()
{
m_isPopup = true;
QFontComboBox::showPopup();
}
void hidePopup()
{
m_isPopup = false;
QFontComboBox::hidePopup();
}
bool event(QEvent* event)
{
if (event->type() == QEvent::Paint)
{
//FIXME: QT5.4.1 通过点击combobox外区域来隐藏列表不会触发 hidePopup() 事件
if (m_isPopup)
{
m_isPopup = false;
}
}
return QComboBox::event(event);
}
protected:
virtual void paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QStyleOptionComboBox opt;
initStyleOption(&opt);
opt.currentText = m_strText;
drawCombo(this, opt);
}
virtual QSize sizeHint() const
{
return QSize(100, Utils::WizStyleHelper::editorButtonHeight());
}
private:
QString m_strText;
bool m_isPopup;
};
class CWizEditorButtonSpliter : public QWidget
{
public:
CWizEditorButtonSpliter(QWidget* parent = 0)
: QWidget(parent)
{
setFixedWidth(1);
setFixedHeight(Utils::WizStyleHelper::editorButtonHeight());
setStyleSheet("background-color:#E7E7E7;");
setAutoFillBackground(true);
}
};
QWidget* createMoveAbleWidget(QWidget* parent)
{
QWidget* moveableButtonContainer = new QWidget(parent);
QHBoxLayout* moveableLayout = new QHBoxLayout(moveableButtonContainer);
moveableLayout->setContentsMargins(0, 0, 0, 0);
moveableLayout->setSpacing(0);
moveableLayout->setAlignment(Qt::AlignVCenter);
moveableButtonContainer->setLayout(moveableLayout);
return moveableButtonContainer;
}
QString commandKey()
{
#ifdef Q_OS_MAC
return "⌘";
#else
return "Ctrl+";
#endif
}
QString optionKey()
{
#ifdef Q_OS_MAC
return "⌥";
#else
return "Alt+";
#endif
}
QString shiftKey()
{
#ifdef Q_OS_MAC
return "⇧";
#else
return "Shift+";
#endif