/
TabWidget.cpp
287 lines (231 loc) · 6.79 KB
/
TabWidget.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
JuffEd - An advanced text editor
Copyright 2007-2010 Mikhail Murzin
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "TabWidget.h"
#include "Document.h"
#include "DocHandlerInt.h"
#include "Functions.h"
#include "IconManager.h"
#include "Log.h"
#include "Project.h"
#include "TabBar.h"
#include <QApplication>
#include <QClipboard>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QFileInfo>
#include <QKeyEvent>
#include <QMenu>
#include <QPushButton>
#include <QUrl>
namespace Juff {
class DocListButton : public QPushButton {
public:
DocListButton(const QIcon& icon) : QPushButton(icon, "") {
setMinimumSize(sz, sz);
setMaximumSize(sz, sz);
setFlat(true);
menu_ = new QMenu();
setMenu(menu_);
}
virtual QSize sizeHint() const {
return QSize(sz, sz);
}
static const int sz;
QMenu* menu_;
};
const int DocListButton::sz = 24;
TabWidget::TabWidget(Juff::DocHandlerInt* handler) : QTabWidget() {
handler_ = handler;
setTabBar(new Juff::TabBar(this));
connect(tabBar(), SIGNAL(tabCloseRequested(int)), SLOT(onTabCloseRequested(int)));
setAcceptDrops(true);
static int ind = 0;
selfIndex_ = ind++;
docListBtn_ = new DocListButton(QIcon());
docListBtn_->setToolTip(tr("Documents list"));
setCornerWidget(docListBtn_, Qt::TopLeftCorner);
docListBtn_->hide();
connect(docListBtn_->menu(), SIGNAL(aboutToShow()), SLOT(onDocListNeedsToBeShown()));
}
QString TabWidget::docName(int index) const {
if ( index < 0 )
return "";
QWidget* tab = widget(index);
Juff::Document* doc = qobject_cast<Juff::Document*>(tab);
if ( doc != 0 )
return doc->fileName();
else
return "";
}
void TabWidget::initDocMenu(int index, QMenu* menu) {
LOGGER;
QWidget* tab = widget(index);
Juff::Document* doc = qobject_cast<Juff::Document*>(tab);
if ( doc != 0 ) {
menuRequestedIndex_ = index;
menu->addAction(tr("Copy file name to clipboard"), this, SLOT(copyFileName()));
if ( !Juff::isNoname(doc) ) {
menu->addAction(tr("Copy full file path to clipboard"), this, SLOT(copyFilePath()));
menu->addAction(tr("Copy file directory path to clipboard"), this, SLOT(copyDirPath()));
}
menu->addSeparator();
// if ( doc->supportsAction(Juff::FileClone) && !doc->hasClone() )
// menu->addAction(tr("Clone to another panel"), this, SLOT(cloneDoc()));
if ( selfIndex_ == 0 )
menu->addAction(tr("Move to the right panel"), this, SLOT(moveDoc()));
else
menu->addAction(tr("Move to the left panel"), this, SLOT(moveDoc()));
Juff::Project* prj = handler_->curPrj();
// TODO : add an accurate check whether the file is a part of the project
if ( prj != 0 && !prj->isNoname() ) {
if ( !prj->files().contains(doc->fileName()) )
menu->addAction(tr("Add to project"), this, SLOT(addFileToProject()))->setIcon(IconManager::instance()->icon(Juff::PrjAddFile));
else
menu->addAction(tr("Remove from project"), this, SLOT(removeFileFromProject()))->setIcon(IconManager::instance()->icon(Juff::PrjRemoveFile));
}
}
else {
menuRequestedIndex_ = -1;
}
}
void TabWidget::copyFileName() {
QString fileName = docName(menuRequestedIndex_);
if ( !fileName.isEmpty() ) {
QString name = QFileInfo(fileName).fileName();
QApplication::clipboard()->setText(name);
}
}
void TabWidget::copyFilePath() {
QString fileName = docName(menuRequestedIndex_);
if ( !fileName.isEmpty() ) {
QApplication::clipboard()->setText(fileName);
}
}
void TabWidget::copyDirPath() {
QString fileName = docName(menuRequestedIndex_);
if ( !fileName.isEmpty() ) {
QString name = QFileInfo(fileName).absolutePath();
QApplication::clipboard()->setText(name);
}
}
void TabWidget::addFileToProject() {
QString fileName = docName(menuRequestedIndex_);
if ( !fileName.isEmpty() ) {
Juff::Project* prj = handler_->curPrj();
prj->addFile(fileName);
}
}
void TabWidget::removeFileFromProject() {
QString fileName = docName(menuRequestedIndex_);
if ( !fileName.isEmpty() ) {
Juff::Project* prj = handler_->curPrj();
prj->removeFile(fileName);
}
}
void TabWidget::onTabCloseRequested(int index) {
LOGGER;
QWidget* tab = widget(index);
Juff::Document* doc = qobject_cast<Juff::Document*>(tab);
if ( doc != 0 ) {
handler_->closeDoc(doc->fileName());
}
}
void TabWidget::onDocListNeedsToBeShown() {
LOGGER;
docListBtn_->menu_->clear();
int n = count();
for ( int i = 0; i < n; ++i ) {
Juff::Document* doc = qobject_cast<Juff::Document*>(widget(i));
if ( doc != 0 ) {
docListBtn_->menu_->addAction(Juff::docIcon(doc), Juff::docTitle(doc), this, SLOT(onDocMenuItemSelected()))->setData(i);
}
}
}
void TabWidget::onDocMenuItemSelected() {
LOGGER;
QAction* act = qobject_cast<QAction*>(sender());
if ( act != 0 ) {
setCurrentIndex(act->data().toInt());
}
}
//void TabWidget::onDocListPressed() {
// LOGGER;
// docListBtn_->men
//}
void TabWidget::tabInserted(int) {
LOGGER;
if ( count() > 0 )
docListBtn_->show();
}
void TabWidget::tabRemoved(int) {
LOGGER;
emit tabRemoved(this);
if ( count() == 0 )
docListBtn_->hide();
}
/*void TabWidget::cloneDoc() {
LOGGER;
QWidget* tab = widget(menuRequestedIndex_);
Juff::Document* doc = qobject_cast<Juff::Document*>(tab);
if ( doc != 0 ) {
emit requestDocClone(doc, this);
}
}*/
void TabWidget::moveDoc() {
LOGGER;
QWidget* tab = widget(menuRequestedIndex_);
Juff::Document* doc = qobject_cast<Juff::Document*>(tab);
if ( doc != 0 ) {
emit requestDocMove(doc, this);
}
}
void TabWidget::dragEnterEvent(QDragEnterEvent* e) {
LOGGER;
if (e->mimeData()->hasUrls()) {
e->acceptProposedAction();
}
}
void TabWidget::dropEvent(QDropEvent* e) {
LOGGER;
if ( e->mimeData()->hasUrls() ) {
QList<QUrl> urls = e->mimeData()->urls();
foreach (QUrl url, urls) {
QString name = url.path();
#ifdef Q_OS_WIN32
// hack to protect of strings with filenames like /C:/doc/file.txt
if ( name[0] == '/' )
name.remove(0, 1);
#endif
if ( !name.isEmpty() ) {
handler_->openDoc(name);
}
}
}
}
void TabWidget::keyPressEvent(QKeyEvent* e) {
if ( e->key() == Qt::Key_Tab || e->key() == Qt::Key_Backtab ) {
if ( e->modifiers() & Qt::ControlModifier ) {
if ( e->modifiers() & Qt::ShiftModifier ) {
emit docStackCalled(false);
}
else {
emit docStackCalled(true);
}
return;
}
}
QTabWidget::keyPressEvent(e);
}
} // namespace Juff