-
Notifications
You must be signed in to change notification settings - Fork 8
/
applicationsview.cpp
201 lines (181 loc) · 6.45 KB
/
applicationsview.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
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
/* BEGIN_COMMON_COPYRIGHT_HEADER
* (c)LGPL2+
*
* LXQt - a lightweight, Qt based, desktop toolset
* https://lxqt-project.org/
*
* Copyright: 2021 LXQt team
*
* This program or library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* END_COMMON_COPYRIGHT_HEADER */
#include "applicationsview.h"
// #include "itemdescriptiondelegate.h"
#include <QMenu>
#include <QProxyStyle>
#include <QScrollBar>
#include <QStandardItemModel>
#include <XdgDesktopFile>
#include <XdgIcon>
#include <QDebug>
class SingleActivateStyle : public QProxyStyle
{
public:
using QProxyStyle::QProxyStyle;
int styleHint(StyleHint hint, const QStyleOption* option = nullptr, const QWidget* widget = nullptr, QStyleHintReturn* returnData = nullptr) const override
{
if (hint == QStyle::SH_ItemView_ActivateItemOnSingleClick)
return 1;
return QProxyStyle::styleHint(hint, option, widget, returnData);
}
};
ApplicationsView::ApplicationsView(int iconSize, AppLayout::Layout appLayout, QWidget* parent)
: QListView(parent), mIconSize(iconSize),
mAppLayout(appLayout),
mDelegate(new ItemDescriptionDelegate),
mDidDrag(false)
{
setMouseTracking(true);
setDragDropMode(QListView::DragOnly);
setContextMenuPolicy(Qt::CustomContextMenu);
setEditTriggers(QAbstractItemView::NoEditTriggers);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollMode(ScrollMode::ScrollPerPixel);
setDefaultDropAction(Qt::MoveAction);
setIconSize(QSize(mIconSize, mIconSize));
setAppLayout(mAppLayout);
setItemDelegate(mDelegate);
}
void ApplicationsView::setAppLayout(AppLayout::Layout layout)
{
int w = 32;
switch (layout) {
case AppLayout::ListNameAndDescription:
setIconSize(QSize(mIconSize, mIconSize));
setGridSize(QSize());
setViewMode(QListView::ViewMode::ListMode);
setWordWrap(false);
w = mIconSize + fontMetrics().averageCharWidth() * 30 + 4;
break;
case AppLayout::ListNameOnly:
setIconSize(QSize(mIconSize, mIconSize));
setGridSize(QSize());
setViewMode(QListView::ViewMode::ListMode);
setWordWrap(false);
w = mIconSize + fontMetrics().averageCharWidth() * 30 + 4;
break;
case AppLayout::Icons:
QSize grid;
auto fm = fontMetrics();
QSize icon(mIconSize * 2, mIconSize * 2);
setIconSize(icon);
// int h = fontMetrics().height();
int textWidth = fm.averageCharWidth() * 13;
int textHeight = fm.lineSpacing() * 3;
grid.setWidth(qMax(icon.width(), textWidth) + 4); // a margin of 2 px for selection rect
grid.setHeight(icon.height() + textHeight + 8); // a margin of 2 px for icon and 2px for text
setGridSize(grid);
setViewMode(QListView::ViewMode::IconMode);
setWordWrap(true);
w = grid.width() * 3 + verticalScrollBar()->sizeHint().width() + 16;
setMovement(QListView::Movement::Static);
break;
}
setMinimumWidth(w);
mAppLayout = layout;
mDelegate->setAppLayout(layout);
}
void ApplicationsView::mousePressEvent(QMouseEvent* event)
{
if (event->button() == Qt::LeftButton) {
mDragStartPosition = event->pos();
mDidDrag = false;
}
QListView::mousePressEvent(event);
}
void ApplicationsView::mouseReleaseEvent(QMouseEvent* e)
{
if ((e->button() == Qt::LeftButton) && (e->pos() - mDragStartPosition).manhattanLength() < QApplication::startDragDistance() && !mDidDrag) {
auto index = indexAt(mDragStartPosition);
auto index2 = indexAt(e->pos());
if (index == index2)
emit indexClicked(index);
}
}
void ApplicationsView::dragEnterEvent(QDragEnterEvent* e)
{
QAbstractItemView::dragEnterEvent(e);
}
void ApplicationsView::dragMoveEvent(QDragMoveEvent* e)
{
QAbstractItemView::dragMoveEvent(e);
}
void ApplicationsView::dragLeaveEvent(QDragLeaveEvent* e)
{
QAbstractItemView::dragLeaveEvent(e);
}
void ApplicationsView::dropEvent(QDropEvent* e)
{
QAbstractItemView::dropEvent(e);
}
void ApplicationsView::keyPressEvent(QKeyEvent* e) {
// ignore everything and let event propagate to WingMenuMenu and then sent to WingMenuWidget
// everything is handled in WingMenuWidget::onKeyPressed
e->ignore();
}
void ApplicationsView::mouseMoveEvent(QMouseEvent* event)
{
// hover
if (event->buttons() == Qt::NoButton) {
auto index = indexAt(event->pos());
if (index.isValid() && !selectedIndexes().contains(index)) {
setCurrentIndex(index);
}
return;
}
// if (mFavorites) {
// QListView::mouseMoveEvent(event);
// return;
// }
if (!(event->buttons() & Qt::LeftButton))
return;
if ((event->pos() - mDragStartPosition).manhattanLength() < QApplication::startDragDistance())
return;
mDidDrag = true;
auto index = indexAt(mDragStartPosition);
if (!index.isValid())
return;
QMimeData* mimeData = new QMimeData();
// if (mFavorites) {
// QString format = QSL("application/x-qabstractitemmodeldatalist");
// QByteArray encoded;
// QDataStream stream(&encoded, QIODevice::WriteOnly);
// stream << index.row() << index.column() << model()->itemData(index);
// mimeData->setData(format, encoded);
// }
QList<QUrl> urls;
urls << QUrl::fromLocalFile(index.data(Qt::UserRole + 3).toString());
mimeData->setUrls(urls);
QDrag* drag = new QDrag(this);
drag->setMimeData(mimeData);
// if (mFavorites)
// drag->exec(Qt::MoveAction | Qt::CopyAction | Qt::LinkAction, Qt::CopyAction);
// else
drag->exec(Qt::CopyAction | Qt::LinkAction);
// mimeData->deleteLater();
// drag->deleteLater();
// emit requestShowHideMenu();
}