This repository has been archived by the owner on Mar 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
zlist.cpp
127 lines (116 loc) · 4.62 KB
/
zlist.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
#include "zlist.h"
#include "daemon.h"
#include "editor.h"
#include <QAbstractItemModelTester>
DGUI_USE_NAMESPACE
ZList::ZList(QWidget* parent)
: QWidget(parent)
{
// qDebug()<<curIdx;
listview = new ZListView(this);
model = new QSortFilterProxyModel(this);
model->setSourceModel(Daemon::instance()->sourceModel());
model->setFilterRole(ZListModel::Attachment);
model->setFilterFixedString("true");
// model->setDynamicSortFilter(false);
// QLoggingCategory::setFilterRules("qt.modeltest.debug=true");
// new QAbstractItemModelTester(model, QAbstractItemModelTester::FailureReportingMode::Fatal, this);
listview->setModel(model);
listview->setNoBackground(true);
DGuiApplicationHelper* guiAppHelp = DGuiApplicationHelper::instance();
themeColor = guiAppHelp->systemTheme()->activeColor();
setLayout(initAddLayer());
listview->setContextMenuPolicy(Qt::CustomContextMenu);
connect(listview, &QListView::customContextMenuRequested, [this](const QPoint& pos) { popupMenu(mapToGlobal(pos)); });
connect(listview, &ZListView::currentIndexChanged, this, &ZList::currentChanged);
connect(listview, &ZListView::listEmptied, [this]() { emit listEmptied(); });
}
QWidget* ZList::initAddButton()
{
auto* layer = new TransparentWidget(this);
layer->setObjectName("toolBarLayer");
QVBoxLayout* layout = new QVBoxLayout(layer);
layer->setLayout(layout);
layout->addStretch();
QHBoxLayout* bar = new QHBoxLayout(layer);
layout->addLayout(bar);
layout->addSpacing(10);
QIcon ico(":/images/add-btn");
auto btn = new DPushButton(ico, "", this);
btn->setObjectName("listAddButton");
btn->setIconSize(QSize(50, 50));
btn->resize(50, 50);
btn->setStyleSheet(QString("#listAddButton{"
"border-radius:25;"
"background-color:%1;"
"margin:10;"
"}"
"#listAddButton:hover{"
"border-radius:25;"
"background-color:%2;"
"margin:10;"
"}"
"#listAddButton:pressed{"
"border-radius:25;"
"background-color:%3;"
"margin:10;"
"}")
.arg(themeColor.name(QColor::HexRgb), themeColor.darker(110).name(QColor::HexRgb),
themeColor.darker(150).name(QColor::HexRgb)));
QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect(btn);
effect->setOffset(0, 0);
effect->setColor(QColor(0, 0, 0, 150));
effect->setBlurRadius(10);
btn->setGraphicsEffect(effect);
connect(btn, &DPushButton::clicked, [this]() { addItem(ZNote()); });
bar->addStretch();
bar->addWidget(btn);
return layer;
}
QLayout* ZList::initAddLayer()
{
QStackedLayout* layout = new QStackedLayout(this);
layout->setStackingMode(QStackedLayout::StackAll);
layout->addWidget(initAddButton());
layout->addWidget(listview);
return layout;
}
void ZList::addItem(const ZNote& item)
{
Daemon::instance()->addItem(item);
auto i = model->index(0, 0);
listview->setCurrentIndex(i);
emit currentChanged(i);
}
void ZList::removeItems(const QList<ZNote>& items)
{
Daemon::instance()->removeItems(items);
}
void ZList::popupMenu(const QPoint& pos)
{
auto selection = listview->selectionNotes();
QMenu* menu = new QMenu();
QAction* removeAction = new QAction(QIcon(":/images/trash-empty"), tr("删除"), menu);
connect(removeAction, &QAction::triggered, [&selection, this]() {
listview->clearSelectionExt();
removeItems(selection);
// listview->setCurrentIndex(indexOf(curIdx));
});
menu->addAction(removeAction);
if (selection.length() == 1) {
QAction* detachAction = new QAction(tr("解除吸附"), menu);
auto index = listview->selection()[0];
connect(detachAction, &QAction::triggered, [index, this]() {
listview->clearSelectionExt();
Daemon::instance()->toggleAttach(index.data(ZListModel::IndexRole).value<InnerIndex>());
// listview->setCurrentIndex(indexOf(curIdx));
});
menu->addAction(detachAction);
}
menu->exec(pos);
}
void ZList::setCurrentIndex(const InnerIndex& idx) { listview->setCurrentIndex(model->mapFromSource(Daemon::instance()->sourceModel()->indexOf(idx))); }
QAbstractItemModel* ZList::sourceModel()
{
return model;
}