-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookmarkDialog.cpp
More file actions
96 lines (82 loc) · 2.69 KB
/
Copy pathBookmarkDialog.cpp
File metadata and controls
96 lines (82 loc) · 2.69 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
#include <Qt>
#include <QDebug>
#include <QStringListModel>
#include "BookmarkDialog.hpp"
#include "ui_BookmarkDialog.h"
class UnEditableStringListModel: public QStringListModel {
public:
virtual Qt::ItemFlags flags(const QModelIndex &index) const {
auto defaultFlags = QStringListModel::flags(index);
if (index.isValid()){
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled;
}
return defaultFlags;
}
private:
};
BookmarkDialog::BookmarkDialog(const QStringList& bookmarks,
QWidget *parent) : QDialog(parent),
ui_(new Ui::BookmarkDialog)
{
ui_ -> setupUi(this);
model_ = new UnEditableStringListModel();
model_ -> setStringList(bookmarks);
ui_ -> bookmarkView -> setModel(model_);
ui_ -> bookmarkView -> setDragEnabled(true);
ui_ -> bookmarkView -> setAcceptDrops(true);
ui_ -> bookmarkView -> setDropIndicatorShown(true);
ui_ -> bookmarkView -> setDragDropMode(QAbstractItemView::InternalMove);
ui_ -> bookmarkView -> setEditTriggers(QAbstractItemView::NoEditTriggers);
ui_ -> bookmarkView -> setDefaultDropAction(Qt::MoveAction);
connectActions();
}
BookmarkDialog::~BookmarkDialog()
{
}
QStringList BookmarkDialog::getBookmarks() const
{
return model_ -> stringList();
}
void BookmarkDialog::connectActions() {
connect(ui_->removeButton, &QPushButton::clicked, [=] {
auto selectedIndexes =
ui_->bookmarkView->selectionModel()->selectedIndexes();
for (const QModelIndex &index : selectedIndexes) {
model_->removeRow(index.row());
}
});
connect(ui_->downButton, &QPushButton::clicked, [=] {
auto selectedIndexes =
ui_->bookmarkView->selectionModel()->selectedIndexes();
auto lst = model_->stringList();
int row = 0;
for (const QModelIndex &index : selectedIndexes) {
if (index.row() + 1 < model_->rowCount()) {
lst.swap(index.row(), index.row() + 1);
row = index.row() + 1;
} else {
row = model_->rowCount() - 1;
}
}
model_->setStringList(lst);
auto index = model_->index(row, 0);
ui_->bookmarkView->setCurrentIndex(index);
});
connect(ui_->upButton, &QPushButton::clicked, [=] {
auto selectedIndexes =
ui_->bookmarkView->selectionModel()->selectedIndexes();
auto lst = model_->stringList();
int row = 0;
for (const QModelIndex &index : selectedIndexes) {
if (index.row() - 1 >= 0) {
lst.swap(index.row(), index.row() - 1);
row = index.row() - 1;
} else {
row = 0;
}
}
model_->setStringList(lst);
auto index = model_->index(row, 0);
ui_->bookmarkView->setCurrentIndex(index);
});
}