-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtaglistwidget.cpp
More file actions
157 lines (133 loc) · 5.03 KB
/
taglistwidget.cpp
File metadata and controls
157 lines (133 loc) · 5.03 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
#include "taglistwidget.h"
TagListWidget::TagListWidget(QWidget *parent)
: QWidget(parent) {
/* Initialize the widgets and variables */
_searchBox = new QLineEdit();
_searchBox->setPlaceholderText(tr("Search"));
_searchBox->setAttribute(Qt::WA_MacShowFocusRect, false);
_searchBox->setObjectName("searchLineEdit");
_view = new QListView;
_view->setSelectionMode(QAbstractItemView::ExtendedSelection);
_view->setAcceptDrops(true);
_view->setDropIndicatorShown(true);
_view->setAttribute(Qt::WA_MacShowFocusRect, false);
_view->setEditTriggers(QAbstractItemView::SelectedClicked);
connect(_view, SIGNAL(doubleClicked(QModelIndex)),
this, SLOT(doubleClicked(QModelIndex)));
//_proxyModel = new QSortFilterProxyModelFixed();
//_proxyModel->setFilterRole(Qt::DisplayRole);
//_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
//connect(_searchBox, SIGNAL(textChanged(QString)),
// _proxyModel, SLOT(setFilterRegExp(QString)));
_proxyModel = new QIdentityProxyModel();
_view->setModel(_proxyModel);
_filterValidateButton = new QPushButton();
_filterValidateButton->setText(tr("Ok"));
_filterValidateButton->hide();
connect(_filterValidateButton, SIGNAL(clicked(bool)),
this, SLOT(requestedFilter()));
/* Defining the layout */
_layout = new QVBoxLayout;
_layout->setSpacing(0);
_layout->setMargin(0);
_layout->addWidget(_searchBox);
_layout->addWidget(_view);
_layout->addWidget(_filterValidateButton);
this->setLayout(_layout);
}
void TagListWidget::setModel(TagListModelDropCheckable *model) {
_model = model;
_proxyModel->setSourceModel(_model);
}
void TagListWidget::connectActions(QMap<QString, QAction *> *actions) {
// General actions
connect(actions->value("add"), SIGNAL(triggered(bool)),
this, SLOT(addElement()));
connect(actions->value("copy"), SIGNAL(triggered(bool)),
this, SLOT(copyElement()));
connect(actions->value("paste"), SIGNAL(triggered(bool)),
this, SLOT(pasteElement()));
connect(actions->value("remove"), SIGNAL(triggered(bool)),
this, SLOT(deleteElement()));
connect(actions->value("find"), SIGNAL(triggered(bool)),
this, SLOT(activateFind()));
// Specific actions
connect(actions->value("rename"), SIGNAL(triggered(bool)),
this, SLOT(renameElement()));
connect(actions->value("filter"), SIGNAL(triggered(bool)),
this, SLOT(filterTags()));
}
void TagListWidget::addElement() {
// Add a new empty tag to the end of the list
QAbstractItemModel *model = _view->model();
model->insertRow(model->rowCount());
_view->edit(model->index(model->rowCount() - 1, 0));
_view->scrollTo(model->index(model->rowCount() - 1, 0));
}
void TagListWidget::deleteElement() {
// Removing element from last to first as we operate on a list
QModelIndexList indexes = _view->selectionModel()->selectedRows();
std::sort(indexes.begin(), indexes.end());
QModelIndexList::reverse_iterator it;
for(it = indexes.rbegin(); it != indexes.rend(); ++it) {
_view->model()->removeRow(it->row(), it->parent());
}
}
void TagListWidget::copyElement() {
qDebug() << "Copy on tags to be implemented";
}
void TagListWidget::pasteElement() {
qDebug() << "Paste on tags to be implemented";
}
void TagListWidget::activateFind() {
// If ctrl+f if pressed setting focus in the search field
_searchBox->setFocus();
}
void TagListWidget::renameElement() {
// Triggers the editting of a selected element
QModelIndexList selectedIndexes = _view->selectionModel()->selectedRows();
if (selectedIndexes.length() <= 0) {
return;
}
std::sort(selectedIndexes.begin(), selectedIndexes.end());
/* Editing the first item if more than one is selected */
_view->edit(selectedIndexes.at(0));
}
void TagListWidget::filterTags() {
// Here the user wants to see the files of multiple tags
// checkboxes are shown in front of the tags
// a "Ok" button appears to validate the selection
filterIsOn = !filterIsOn;
_model->toggleFilter(filterIsOn);
if (filterIsOn) {
_filterValidateButton->show();
} else {
_filterValidateButton->hide();
}
}
void TagListWidget::requestedFilter() {
// The user activated the filter mode and clicked on "Ok"
// we show the files the user asked for
QModelIndexList *indexes = _model->getChecked();
if (indexes->length() == 0) {
return;
}
emit viewFileList(indexes);
}
void TagListWidget::doubleClicked(QModelIndex index) {
// The user double clicked on an item
// we show the files associated with the tag double clicked
if (!index.isValid()) {
return;
}
/* We want to change view so we SIGNAL it */
QModelIndexList *indexes = new QModelIndexList();
indexes->append(index);
emit viewFileList(indexes);
}
TagListWidget::~TagListWidget() {
delete _layout;
delete _view;
delete _searchBox;
delete _proxyModel;
}