-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathWAbstractItemView.C
More file actions
1832 lines (1490 loc) · 48.9 KB
/
WAbstractItemView.C
File metadata and controls
1832 lines (1490 loc) · 48.9 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
/*
* Copyright (C) 2010 Emweb bv, Herent, Belgium.
*
* See the LICENSE file for terms of use.
*/
#include "Wt/WAbstractItemModel.h"
#include "Wt/WAbstractItemView.h"
#include "Wt/WAbstractTableModel.h"
#include "Wt/WApplication.h"
#include "Wt/WContainerWidget.h"
#include "Wt/WEnvironment.h"
#include "Wt/WEvent.h"
#include "Wt/WImage.h"
#include "Wt/WItemSelectionModel.h"
#include "Wt/WItemDelegate.h"
#include "Wt/WLogger.h"
#include "Wt/WPushButton.h"
#include "Wt/WText.h"
#include "Wt/WTheme.h"
#include "SizeHandle.h"
#include "WebUtils.h"
namespace Wt {
WT_MAYBE_UNUSED LOGGER("WAbstractItemView");
class DefaultPagingBar : public WContainerWidget
{
public:
DefaultPagingBar(WAbstractItemView *view)
: view_(view)
{
view_->addStyleClass("Wt-itemview-paged");
setStyleClass("Wt-pagingbar");
firstButton_ = addWidget(
std::make_unique<WPushButton>(
tr("Wt.WAbstractItemView.PageBar.First")));
firstButton_->clicked().connect(this, &DefaultPagingBar::showFirstPage);
prevButton_ = addWidget(
std::make_unique<WPushButton>(
tr("Wt.WAbstractItemView.PageBar.Previous")));
prevButton_->clicked().connect(this, &DefaultPagingBar::showPreviousPage);
current_ = addWidget(std::make_unique<WText>());
nextButton_ = addWidget(
std::make_unique<WPushButton>(
tr("Wt.WAbstractItemView.PageBar.Next")));
nextButton_->clicked().connect(this, &DefaultPagingBar::showNextPage);
lastButton_ = addWidget(
std::make_unique<WPushButton>(
tr("Wt.WAbstractItemView.PageBar.Last")));
lastButton_->clicked().connect(this, &DefaultPagingBar::showLastPage);
view_->pageChanged().connect(this, &DefaultPagingBar::update);
update();
}
private:
WAbstractItemView *view_;
observing_ptr<WPushButton> prevButton_, nextButton_,
firstButton_, lastButton_;
observing_ptr<WText> current_;
void update() {
firstButton_->setDisabled(view_->currentPage() == 0);
prevButton_->setDisabled(view_->currentPage() == 0);
nextButton_->setDisabled(view_->currentPage() == view_->pageCount() - 1);
lastButton_->setDisabled(view_->currentPage() == view_->pageCount() - 1);
current_->setText(WString::tr("Wt.WAbstractItemView.PageIOfN").
arg(view_->currentPage() + 1).arg(view_->pageCount()));
}
void showFirstPage()
{
view_->setCurrentPage(0);
}
void showLastPage()
{
view_->setCurrentPage(view_->pageCount() - 1);
}
void showPreviousPage()
{
if (view_->currentPage() > 0)
view_->setCurrentPage(view_->currentPage() - 1);
}
void showNextPage()
{
if (view_->currentPage() < view_->pageCount() - 1)
view_->setCurrentPage(view_->currentPage() + 1);
}
};
class HeaderProxyModel : public WAbstractTableModel
{
public:
HeaderProxyModel(const std::shared_ptr<WAbstractItemModel>& model)
: model_(model)
{ }
virtual int columnCount(WT_MAYBE_UNUSED const WModelIndex& parent = WModelIndex()) const
override
{
return model_->columnCount();
}
virtual int rowCount(WT_MAYBE_UNUSED const WModelIndex& parent = WModelIndex()) const
override
{
return 1;
}
virtual cpp17::any data(const WModelIndex& index,
ItemDataRole role = ItemDataRole::Display) const override
{
return model_->headerData(index.column(), Orientation::Horizontal, role);
}
virtual bool setData(const WModelIndex& index, const cpp17::any& value,
ItemDataRole role = ItemDataRole::Edit) override
{
return model_->setHeaderData(index.column(), Orientation::Horizontal,
value, role);
}
virtual WFlags<ItemFlag> flags(const WModelIndex& index) const override
{
WFlags<HeaderFlag> headerFlags
= model_->headerFlags(index.column(), Orientation::Horizontal);
WFlags<ItemFlag> result;
if (headerFlags.test(HeaderFlag::UserCheckable))
result |= ItemFlag::UserCheckable;
if (headerFlags.test(HeaderFlag::Tristate))
result |= ItemFlag::Tristate;
if (headerFlags.test(HeaderFlag::XHTMLText))
result |= ItemFlag::XHTMLText;
return result;
}
private:
std::shared_ptr<WAbstractItemModel> model_;
};
WAbstractItemView::ColumnInfo::ColumnInfo(const WAbstractItemView *view,
int anId)
: id(anId),
sortOrder(SortOrder::Ascending),
alignment(AlignmentFlag::Left),
headerHAlignment(AlignmentFlag::Left),
headerVAlignment(view->defaultHeaderVAlignment_),
headerWordWrap(view->defaultHeaderWordWrap_),
extraHeaderWidget(nullptr),
sorting(view->sorting_),
hidden(false),
resizable(view->columnResize_),
itemDelegate_(nullptr)
{
width = WLength(150);
std::unique_ptr<WCssTemplateRule> r
(new WCssTemplateRule("#" + view->id() + " ." + styleClass()));
r->templateWidget()->resize(width.toPixels(), WLength::Auto);
styleRule = r.get();
WApplication::instance()->styleSheet().addRule(std::move(r));
}
int WAbstractItemView::visibleColumnIndex(int modelColumn) const
{
if (columns_[modelColumn].hidden)
return -1;
int j = 0;
for (int i = 0; i < modelColumn; ++i)
if (!columns_[i].hidden)
++j;
return j;
}
int WAbstractItemView::modelColumnIndex(int visibleColumn) const
{
int j = -1;
for (int i = 0; i <= visibleColumn; ++i) {
++j;
while (static_cast<unsigned>(j) < columns_.size() && columns_[j].hidden)
++j;
if (static_cast<unsigned>(j) >= columns_.size())
return -1;
}
return j;
}
std::string WAbstractItemView::ColumnInfo::styleClass() const
{
#ifndef WT_TARGET_JAVA
char buf[40];
buf[0] = 0;
std::strcat(buf, "Wt-tv-c");
Utils::itoa(id, buf + 7, 10);
return buf;
#else
return "Wt-tv-c" + std::to_string(id);
#endif
}
WAbstractItemView::WAbstractItemView()
: WCompositeWidget(std::unique_ptr<WWidget>(new WContainerWidget())),
renderState_(RenderState::NeedRerender),
currentSortColumn_(-1),
dragEnabled_(false),
selectionModel_(new WItemSelectionModel()),
rowHeight_(20),
headerLineHeight_(20),
selectionMode_(SelectionMode::None),
sorting_(true),
columnResize_(true),
defaultHeaderVAlignment_(AlignmentFlag::Middle),
defaultHeaderWordWrap_(true),
rowHeaderCount_(0),
sortEnabled_(true),
columnWidthChanged_(implementation(), "columnResized"),
nextColumnId_(1),
alternatingRowColors_(false),
editTriggers_(EditTrigger::DoubleClicked),
editOptions_(EditOption::SingleEditor),
touchRegistered_(false)
{
impl_ = dynamic_cast<WContainerWidget *>(implementation());
auto d = std::make_shared<WItemDelegate>();
setItemDelegate(d);
setHeaderItemDelegate(d);
WApplication *app = WApplication::instance();
SizeHandle::loadJavaScript(app);
if (!app->environment().ajax())
columnResize_ = false;
bindObjJS(resizeHandleMDownJS_, "resizeHandleMDown");
headerHeightRule_ = new WCssTemplateRule("#" + id() + " .headerrh");
app->styleSheet().addRule(std::unique_ptr<WCssRule>(headerHeightRule_.get()));
setHeaderHeight(headerLineHeight_);
}
WAbstractItemView::~WAbstractItemView()
{
WApplication *app = WApplication::instance();
app->styleSheet().removeRule(headerHeightRule_.get());
for (unsigned i = 0; i < columns_.size(); ++i)
app->styleSheet().removeRule(columns_[i].styleRule.get());
}
void WAbstractItemView
::setModel(const std::shared_ptr<WAbstractItemModel>& model)
{
if (!columnWidthChanged_.isConnected())
columnWidthChanged_.connect(this, &WAbstractItemView::updateColumnWidth);
bool isReset = model_.get();
/* disconnect slots from previous model */
for (unsigned i = 0; i < modelConnections_.size(); ++i)
modelConnections_[i].disconnect();
modelConnections_.clear();
model_ = model;
headerModel_.reset(new HeaderProxyModel(model_));
auto oldSelectionModel = std::move(selectionModel_);
selectionModel_.reset(new WItemSelectionModel(model));
selectionModel_->setSelectionBehavior(oldSelectionModel->selectionBehavior());
delayedClearAndSelectIndex_ = WModelIndex();
editedItems_.clear();
if (!isReset)
initDragDrop();
configureModelDragDrop();
setRootIndex(WModelIndex());
setHeaderHeight(headerLineHeight_);
}
void WAbstractItemView::setRootIndex(const WModelIndex& rootIndex)
{
rootIndex_ = rootIndex;
scheduleRerender(RenderState::NeedRerender);
unsigned modelColumnCount = model_->columnCount(rootIndex_);
WApplication *app = WApplication::instance();
while (columns_.size() > modelColumnCount) {
int i = columns_.size() - 1;
app->styleSheet().removeRule(columns_[i].styleRule.get());
columns_.erase(columns_.begin() + i);
}
while (columns_.size() < modelColumnCount)
columns_.push_back(createColumnInfo(columns_.size()));
}
void WAbstractItemView::setRowHeight(const WLength& rowHeight)
{
rowHeight_ = rowHeight;
}
void WAbstractItemView::setRowHeaderCount(int count)
{
rowHeaderCount_ = count;
}
WLength WAbstractItemView::columnWidth(int column) const
{
return columnInfo(column).width;
}
void WAbstractItemView::updateColumnWidth(int columnId, int width)
{
int column = columnById(columnId);
if (column >= 0) {
columnInfo(column).width = width;
columnResized_.emit(column, columnInfo(column).width);
WWidget *w = headerWidget(column, 0);
if (w)
w->scheduleRender(RepaintFlag::SizeAffected); // for layout
}
}
void WAbstractItemView::setColumnHidden(int column, bool hidden)
{
columnInfo(column).hidden = hidden;
}
bool WAbstractItemView::isColumnHidden(int column) const
{
return columnInfo(column).hidden;
}
void WAbstractItemView::hideColumn(int column)
{
setColumnHidden(column, true);
}
void WAbstractItemView::showColumn(int column)
{
setColumnHidden(column, false);
}
void WAbstractItemView::initDragDrop()
{
/* item drag & drop */
addCssRule
("#" + id() + "dw",
"width: 32px; height: 32px;"
"background: url(" + WApplication::resourcesUrl() + "items-not-ok.gif);");
addCssRule
("#" + id() + "dw.Wt-valid-drop",
"width: 32px; height: 32px;"
"background: url(" + WApplication::resourcesUrl() + "items-ok.gif);");
selectionChanged_.connect(this, &WAbstractItemView::checkDragSelection);
}
void WAbstractItemView::setColumnResizeEnabled(bool enabled)
{
columnResize_ = enabled;
for (unsigned int i = 0; i < static_cast<unsigned int>(columns_.size()); i++){
columnInfo(i).resizable = enabled;
}
scheduleRerender(RenderState::NeedRerenderHeader);
}
void WAbstractItemView::setColumnResizeEnabled(bool enabled, int column)
{
if (enabled != columnInfo(column).resizable) {
columnInfo(column).resizable = enabled;
scheduleRerender(RenderState::NeedRerenderHeader);
}
}
void WAbstractItemView::setColumnAlignment(int column, AlignmentFlag alignment)
{
columnInfo(column).alignment = alignment;
WApplication *app = WApplication::instance();
const char *align = nullptr;
switch (alignment) {
case AlignmentFlag::Left:
align = app->layoutDirection() == LayoutDirection::LeftToRight
? "left" : "right";
break;
case AlignmentFlag::Center: align = "center"; break;
case AlignmentFlag::Right:
align = app->layoutDirection() == LayoutDirection::LeftToRight
? "right" : "left";
break;
case AlignmentFlag::Justify: align = "justify"; break;
default:
break;
}
if (align) {
WWidget *w = columnInfo(column).styleRule->templateWidget();
w->setAttributeValue("style", std::string("text-align: ") + align);
}
}
AlignmentFlag WAbstractItemView::columnAlignment(int column) const
{
return columnInfo(column).alignment;
}
void WAbstractItemView::setHeaderAlignment(int column,
WFlags<AlignmentFlag> alignment)
{
columnInfo(column).headerHAlignment = alignment & AlignHorizontalMask;
if (!(alignment & AlignVerticalMask).empty())
columnInfo(column).headerVAlignment = alignment & AlignVerticalMask;
if (columnInfo(column).hidden ||
static_cast<unsigned int>(renderState_) >=
static_cast<unsigned int>(RenderState::NeedRerenderHeader))
return;
WContainerWidget *wc = dynamic_cast<WContainerWidget *>(headerWidget(column));
wc->setContentAlignment(alignment);
if (columnInfo(column).headerVAlignment == AlignmentFlag::Middle)
wc->setLineHeight(headerLineHeight_);
else
wc->setLineHeight(WLength::Auto);
}
void WAbstractItemView::setHeaderWordWrap(int column, bool enabled)
{
columnInfo(column).headerWordWrap = enabled;
if (columnInfo(column).hidden ||
static_cast<unsigned int>(renderState_) >=
static_cast<unsigned int>(RenderState::NeedRerenderHeader))
return;
if (columnInfo(column).headerVAlignment == AlignmentFlag::Top) {
WContainerWidget *wc
= dynamic_cast<WContainerWidget *>(headerWidget(column));
wc->toggleStyleClass("Wt-wwrap", enabled);
}
}
AlignmentFlag WAbstractItemView::horizontalHeaderAlignment(int column) const
{
return columnInfo(column).headerHAlignment;
}
AlignmentFlag WAbstractItemView::verticalHeaderAlignment(int column) const
{
return columnInfo(column).headerVAlignment;
}
void WAbstractItemView::setAlternatingRowColors(bool enable)
{
alternatingRowColors_ = enable;
}
void WAbstractItemView::setSelectionMode(SelectionMode mode)
{
if (mode != selectionMode_) {
clearSelection();
selectionMode_ = mode;
}
}
void WAbstractItemView::setSelectionBehavior(SelectionBehavior behavior)
{
if (behavior != selectionBehavior()) {
clearSelection();
selectionModel_->setSelectionBehavior(behavior);
}
}
SelectionBehavior WAbstractItemView::selectionBehavior() const
{
return selectionModel_->selectionBehavior();
}
void WAbstractItemView
::setItemDelegate(const std::shared_ptr<WAbstractItemDelegate>& delegate)
{
itemDelegate_ = delegate;
itemDelegate_->closeEditor()
.connect(this, &WAbstractItemView::closeEditorWidget);
}
void WAbstractItemView
::setItemDelegateForColumn(int column,
const std::shared_ptr<WAbstractItemDelegate>& delegate)
{
columnInfo(column).itemDelegate_ = delegate;
delegate->closeEditor()
.connect(this, &WAbstractItemView::closeEditorWidget);
}
std::shared_ptr<WAbstractItemDelegate> WAbstractItemView
::itemDelegateForColumn(int column)
const
{
return columnInfo(column).itemDelegate_;
}
std::shared_ptr<WAbstractItemDelegate> WAbstractItemView
::itemDelegate(const WModelIndex& index)
const
{
return itemDelegate(index.column());
}
std::shared_ptr<WAbstractItemDelegate> WAbstractItemView
::itemDelegate(int column) const
{
auto result = itemDelegateForColumn(column);
return result ? result : itemDelegate_;
}
void WAbstractItemView::
setHeaderItemDelegate(const std::shared_ptr<WAbstractItemDelegate>& delegate)
{
headerItemDelegate_ = delegate;
}
std::shared_ptr<WAbstractItemDelegate> WAbstractItemView::headerItemDelegate()
const
{
return headerItemDelegate_;
}
std::string repeat(const std::string& s, int times)
{
std::string result;
for (int i = 0; i < times; ++i) {
result += s;
}
return result;
}
void WAbstractItemView::dropEvent(const WDropEvent& e, const WModelIndex& index)
{
/*
* Here, we only handle standard drag&drop actions between abstract
* item models.
*/
if (enabledDropLocations_.test(DropLocation::OnItem)) {
std::vector<std::string> acceptMimeTypes = model_->acceptDropMimeTypes();
for (std::size_t i = 0; i < acceptMimeTypes.size(); ++i) {
if (acceptMimeTypes[i] == e.mimeType()) {
// we define internal by sharing the same selection model...
// currently selection models cannot be shared
bool internal = e.source() == selectionModel_.get();
DropAction action = internal ?
DropAction::Move : DropAction::Copy;
model_->dropEvent(e, action,
index.row(), index.column(), index.parent());
setSelectedIndexes(WModelIndexSet());
return;
}
}
}
WCompositeWidget::dropEvent(e);
}
void WAbstractItemView::dropEvent(const WDropEvent& e, const WModelIndex& index, Side side)
{
if (enabledDropLocations_.test(DropLocation::BetweenRows)) {
std::vector<std::string> acceptMimeTypes = model_->acceptDropMimeTypes();
for (std::size_t i = 0; i < acceptMimeTypes.size(); ++i) {
if (acceptMimeTypes[i] == e.mimeType()) {
model_->dropEvent(e, DropAction::Move, index, side);
setSelectedIndexes(WModelIndexSet());
return;
}
}
}
WCompositeWidget::dropEvent(e);
}
void WAbstractItemView::configureModelDragDrop()
{
if (!model_)
return;
if (dragEnabled_) {
setAttributeValue
("dsid",
WApplication::instance()->encodeObject(selectionModel_.get()));
checkDragSelection();
}
std::vector<std::string> acceptMimeTypes = model_->acceptDropMimeTypes();
for (unsigned i = 0; i < acceptMimeTypes.size(); ++i)
if (!enabledDropLocations_.empty())
acceptDrops(acceptMimeTypes[i], "Wt-drop-site");
else
stopAcceptDrops(acceptMimeTypes[i]);
}
void WAbstractItemView::setDropsEnabled(bool enable)
{
if (enable)
setEnabledDropLocations(DropLocation::OnItem);
else
setEnabledDropLocations(None);
}
void WAbstractItemView::setEnabledDropLocations(WFlags<DropLocation> dropLocations) {
if (enabledDropLocations_ == dropLocations)
return;
enabledDropLocations_ = dropLocations;
configureModelDragDrop();
scheduleRender();
}
void WAbstractItemView::checkDragSelection()
{
/*
* Check whether the current selection can be drag and dropped
*/
computedDragMimeType_ = selectionModel_->mimeType();
setAttributeValue("dmt", computedDragMimeType_);
if (!computedDragMimeType_.empty())
setAttributeValue("drag", "true");
else
setAttributeValue("drag", "false");
}
WText *WAbstractItemView::headerSortIconWidget(int column)
{
if (!columnInfo(column).sorting)
return nullptr;
WWidget *hw = headerWidget(column);
if (hw)
return dynamic_cast<WText *>(hw->find("sort"));
else
return nullptr;
}
std::unique_ptr<WWidget> WAbstractItemView::createExtraHeaderWidget(WT_MAYBE_UNUSED int column)
{
return std::unique_ptr<WWidget>();
}
WWidget *WAbstractItemView::extraHeaderWidget(int column)
{
return columnInfo(column).extraHeaderWidget.get();
}
void WAbstractItemView::handleHeaderClicked(int columnid, WMouseEvent event)
{
int column = columnById(columnid);
ColumnInfo& info = columnInfo(column);
if (sortEnabled_ && info.sorting)
toggleSortColumn(columnid);
headerClicked_.emit(column, event);
}
void WAbstractItemView::handleHeaderDblClicked(int columnid, WMouseEvent event)
{
headerDblClicked_.emit(columnById(columnid), event);
}
void WAbstractItemView::handleHeaderMouseDown(int columnid, WMouseEvent event)
{
headerMouseWentDown_.emit(columnById(columnid), event);
}
void WAbstractItemView::handleHeaderMouseUp(int columnid, WMouseEvent event)
{
headerMouseWentUp_.emit(columnById(columnid), event);
}
void WAbstractItemView::toggleSortColumn(int columnid)
{
int column = columnById(columnid);
if (column != currentSortColumn_)
sortByColumn(column, columnInfo(column).sortOrder);
else
sortByColumn(column,
columnInfo(column).sortOrder == SortOrder::Ascending
? SortOrder::Descending : SortOrder::Ascending);
}
int WAbstractItemView::sortColumn() const
{
return currentSortColumn_;
}
SortOrder WAbstractItemView::sortOrder() const
{
if (currentSortColumn_ >= 0
&& currentSortColumn_ < static_cast<int>(columns_.size()))
return columns_[currentSortColumn_].sortOrder;
else
return SortOrder::Ascending;
}
int WAbstractItemView::columnById(int columnid) const
{
for (unsigned i = 0; i < columns_.size(); ++i)
if (columnInfo(i).id == columnid)
return i;
return 0;
}
int WAbstractItemView::columnCount() const
{
return columns_.size();
}
int WAbstractItemView::visibleColumnCount() const
{
int result = 0;
for (unsigned i = 0; i < columns_.size(); ++i)
if (!columns_[i].hidden)
++result;
return result;
}
WAbstractItemView::ColumnInfo WAbstractItemView::createColumnInfo(WT_MAYBE_UNUSED int column) const
{
return ColumnInfo(this, nextColumnId_++);
}
WAbstractItemView::ColumnInfo& WAbstractItemView::columnInfo(int column) const
{
while (column >= (int)columns_.size())
columns_.push_back(createColumnInfo(columns_.size()));
return columns_[column];
}
void WAbstractItemView::sortByColumn(int column, SortOrder order)
{
if (currentSortColumn_ != -1) {
WText* t = headerSortIconWidget(currentSortColumn_);
if (t)
t->setStyleClass("Wt-tv-sh Wt-tv-sh-none");
}
currentSortColumn_ = column;
columnInfo(column).sortOrder = order;
if (renderState_ != RenderState::NeedRerender) {
WText* t = headerSortIconWidget(currentSortColumn_);
if (t)
t->setStyleClass(order == SortOrder::Ascending
? "Wt-tv-sh Wt-tv-sh-up" : "Wt-tv-sh Wt-tv-sh-down");
}
model_->sort(column, order);
}
void WAbstractItemView::setSortingEnabled(bool enabled)
{
sorting_ = enabled;
for (unsigned i = 0; i < columns_.size(); ++i)
columnInfo(i).sorting = enabled;
scheduleRerender(RenderState::NeedRerenderHeader);
}
void WAbstractItemView::setSortingEnabled(int column, bool enabled)
{
columnInfo(column).sorting = enabled;
scheduleRerender(RenderState::NeedRerenderHeader);
}
bool WAbstractItemView::isSortingEnabled(int column) const
{
return columnInfo(column).sorting;
}
void WAbstractItemView::modelReset()
{
setModel(model_);
}
bool WAbstractItemView::internalSelect(const WModelIndex& index,
SelectionFlag option)
{
if (!(index.flags() & ItemFlag::Selectable) ||
selectionMode() == SelectionMode::None)
return false;
if (option == SelectionFlag::ToggleSelect)
option = isSelected(index)
? SelectionFlag::Deselect : SelectionFlag::Select;
if (selectionMode() == SelectionMode::Single &&
option == SelectionFlag::Select)
option = SelectionFlag::ClearAndSelect;
if ((option == SelectionFlag::ClearAndSelect ||
option == SelectionFlag::Select) &&
selectionModel()->selection_.size() == 1 &&
isSelected(index))
return false;
else if (option == SelectionFlag::Deselect && !isSelected(index))
return false;
if (option == SelectionFlag::ClearAndSelect) {
clearSelection();
option = SelectionFlag::Select;
}
/*
* now option is either SelectionFlag::Select or SelectionFlag::Deselect and we only need to do
* exactly that one thing
*/
if (option == SelectionFlag::Select)
selectionModel()->selection_.insert(index);
else
selectionModel()->selection_.erase(index);
return true;
}
void WAbstractItemView::clearSelection()
{
WModelIndexSet& nodes = selectionModel_->selection_;
while (!nodes.empty()) {
WModelIndex i = *nodes.begin();
internalSelect(i, SelectionFlag::Deselect);
}
}
void WAbstractItemView::setSelectedIndexes(const WModelIndexSet& indexes)
{
if (indexes.empty() && selectionModel_->selection_.empty())
return;
clearSelection();
for (WModelIndexSet::const_iterator i = indexes.begin();
i != indexes.end(); ++i)
internalSelect(*i, SelectionFlag::Select);
selectionChanged_.emit();
}
void WAbstractItemView::extendSelection(const WModelIndex& index)
{
if (selectionModel_->selection_.empty())
internalSelect(index, SelectionFlag::Select);
else {
if (selectionBehavior() == SelectionBehavior::Rows &&
index.column() != 0) {
extendSelection(model_->index(index.row(), 0, index.parent()));
return;
}
}
/*
* Expand current selection. If index is within or below the
* current selection, we select from the top item to index. If index
* is above the current selection, select everything from the
* bottom item to index.
*
* For a WTreeView, only indexes with expanded ancestors can be
* part of the selection: this is asserted when collapsing a index.
*/
WModelIndex top = Utils::first(selectionModel_->selection_);
if (top < index) {
clearSelection();
selectRange(top, index);
} else {
WModelIndex bottom = Utils::last(selectionModel_->selection_);
clearSelection();
selectRange(index, bottom);
}
selectionChanged_.emit();
}
void WAbstractItemView::extendSelection(const std::vector<WModelIndex>& indices)
{
const WModelIndex &firstIndex = indices[0];
const WModelIndex &secondIndex = indices[indices.size()-1];
if (indices.size() > 1) {
if(firstIndex.row() > secondIndex.row())
selectRange(secondIndex, firstIndex);
else
selectRange(firstIndex, secondIndex);
}
selectionChanged_.emit();
}
bool WAbstractItemView::isSelected(const WModelIndex& index) const
{
return selectionModel_->isSelected(index);
}
void WAbstractItemView::select(const WModelIndex& index, SelectionFlag option)
{
if (internalSelect(index, option))
selectionChanged_.emit();
}
void WAbstractItemView::selectionHandleClick(const WModelIndex& index,
WFlags<KeyboardModifier> modifiers)
{
if (selectionMode_ == SelectionMode::None)
return;
if (selectionMode_ == SelectionMode::Extended) {
if (modifiers.test(KeyboardModifier::Shift))
extendSelection(index);
else {
if (!(modifiers & (KeyboardModifier::Control |
KeyboardModifier::Meta))) {
if (!dragEnabled_)
select(index, SelectionFlag::ClearAndSelect);
else {
if (!isSelected(index))
select(index, SelectionFlag::ClearAndSelect);
else
delayedClearAndSelectIndex_ = index;
}
} else
select(index, SelectionFlag::ToggleSelect);
}
} else {
if (!(modifiers & (KeyboardModifier::Control |
KeyboardModifier::Meta)).empty() &&
isSelected(index)) {
clearSelection();
selectionChanged_.emit();
} else
select(index, SelectionFlag::Select);
}
}