forked from baumgarr/nixnote2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidgetpanel.cpp
185 lines (142 loc) · 5.02 KB
/
widgetpanel.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
/*********************************************************************************
NixNote - An open-source client for the Evernote service.
Copyright (C) 2013 Randy Baumgarte
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
***********************************************************************************/
#include "widgetpanel.h"
#include <QVBoxLayout>
#include <QStyleOption>
#include <QStyle>
#include <QPainter>
#include <QScrollArea>
#include <QDragMoveEvent>
#include "global.h"
extern Global global;
WidgetPanel::WidgetPanel(QWidget *parent) :
QWidget(parent)
{
vboxLayout = new QVBoxLayout();
this->setLayout(vboxLayout);
vboxLayout->setSpacing(10);
vboxLayout->setSizeConstraint(QLayout::SetNoConstraint);
this->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
this->setStyleSheet("QTreeView {background:transparent; border:none; margin:0px; padding: 0px;} ");
this->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
connect(&timer, SIGNAL(timeout()), this, SLOT(scrollTimer()));
}
WidgetPanel::~WidgetPanel() {
delete vboxLayout;
}
void WidgetPanel::addWidget(QWidget *widget) {
widget->setParent(this);
vboxLayout->addWidget(widget);
}
void WidgetPanel::paintEvent(QPaintEvent *e)
{
//Suppress unused
Q_UNUSED(e);
return;
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
QSize WidgetPanel::sizeHint() {
return minimumSizeHint();
}
void WidgetPanel::addFavoritesView(FavoritesView *view) {
view->setObjectName("Favorites");
this->favoritesView = view;
addWidget(view);
}
void WidgetPanel::addNotebookView(NNotebookView *view) {
view->setObjectName("Notebooks");
notebookView = view;
addWidget(view);
}
void WidgetPanel::addTagView(NTagView *view) {
view->setObjectName("Tags");
tagView = view;
addWidget(view);
}
void WidgetPanel::addSearchView(NSearchView *view) {
view->setObjectName("SavedSearches");
searchView = view;
addWidget(view);
}
void WidgetPanel::addTrashTree(NTrashTree *view) {
view->setObjectName("Trash");
trashTree = view;
addWidget(view);
}
void WidgetPanel::addAttributeTree(NAttributeTree *view) {
view->setObjectName("Attributes");
attributeTree = view;
addWidget(view);
}
void WidgetPanel::addSeparator(QLabel *separator) {
addWidget(separator);
}
void WidgetPanel::scrollTimer() {
QScrollArea *scrollArea = (QScrollArea*)this->parentWidget();
if (scrollUp) {
int visibleY = this->visibleRegion().boundingRect().y();
if (visibleY == 0) {
timer.stop();
return;
}
scrollArea->scroll(0,1);
} else {
bool bottomVisible = this->visibleRegion().contains(tagView->geometry().bottomLeft());
if (!bottomVisible)
scrollArea->scroll(0,-1);
else
timer.stop();
}
}
void WidgetPanel::dragEnterHandler(QDragEnterEvent *event) {
Q_UNUSED(event);
priorMousePosition = QCursor::pos();
}
void WidgetPanel::dragMoveHandler(QDragMoveEvent *event) {
//QPoint pos = event->source()->mapToGlobal(event->pos());
QWidget *source = qobject_cast<QWidget *>(event->source());
QPoint pos = QCursor::pos();
pos = this->mapFromGlobal(pos);
int mouse = QCursor::pos().y();
// Check if we are close to the top. If so, then start scrolling up.
if (pos.y()<priorMousePosition.y()) {
int visibleY = this->visibleRegion().boundingRect().y();
int top = this->mapFromGlobal(this->geometry().topLeft()).y() *-1;
if (!scrollUp)
timer.stop();
if (mouse-top < 50 && visibleY > 0) {
scrollUp = true;
timer.start(5);
}
}
// Check if we are close to the bottom. If so, start scrolling down
if (pos.y()>priorMousePosition.y()) {
timer.stop();
scrollUp=false;
if (source->objectName()=="Tags") {
int bottom = source->mapToGlobal(source->visibleRegion().boundingRect().bottomLeft()).y();
int actualBottom = source->mapToGlobal(source->geometry().bottomLeft()).y();
bool bottomVisible = this->visibleRegion().contains(source->geometry().bottomLeft());
if (bottom-mouse < 50 && bottom < actualBottom && !bottomVisible) {
timer.start(5);
}
}
}
priorMousePosition = pos;
}