-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
330 lines (283 loc) · 9.51 KB
/
mainwindow.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include "mainwindow.h"
#include "notebox.h"
//#include <QApplication>
//#include <QToolBar>
//#include <QTabWidget>
//#include <QMenuBar>
//#include <QMenu>
//#include <QTextEdit>
//#include <QClipboard>
//#include <QtGui> //includes all the above
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
tabinc = 0;
//
// file actions
//
//QToolBar *tb = addToolBar(tr("File Actions")); //draggable toolbar
QMenu *menu = new QMenu(tr("File"), this);
menuBar()->addMenu(menu);
QAction *a;
a = new QAction(tr("&New"), this);
a->setShortcut(QKeySequence::New);
connect(a, SIGNAL(triggered()), this, SLOT(fileNew()));
//tb->addAction(a);
menu->addAction(a);
//note: these shortcuts should be editable
a = new QAction(tr("&Open"), this);
a->setShortcut(QKeySequence::Open);
connect(a, SIGNAL(triggered()), SLOT(fileOpen()));
menu->addAction(a);
a = actionSave = new QAction(tr("&Save"), this);
a->setShortcut(QKeySequence::Save);
connect(a, SIGNAL(triggered()), SLOT(fileSave()));
menu->addAction(a);
a = new QAction(tr("Sa&ve As..."), this);
connect(a, SIGNAL(triggered()), SLOT(fileSaveAs()));
menu->addAction(a);
a = new QAction(tr("Close Tab"), this);
a->setShortcut(Qt::CTRL + Qt::Key_W);
connect(a, SIGNAL(triggered()), SLOT(closeTab()));
menu->addAction(a);
menu->addSeparator();
a = new QAction(tr("&Quit"), this);
a->setShortcut(Qt::CTRL + Qt::Key_Q);
connect(a, SIGNAL(triggered()), SLOT(close()));
menu->addAction(a);
//
// edit actions
//
menu = new QMenu(tr("Edit"), this);
menuBar()->addMenu(menu);
a = actionUndo = new QAction(tr("&Undo"), this);
a->setShortcut(QKeySequence::Undo);
menu->addAction(a);
a = actionRedo = new QAction(tr("&Redo"), this);
a->setShortcut(QKeySequence::Redo);
menu->addAction(a);
menu->addSeparator();
a = actionCut = new QAction(tr("Cut (&x)"), this);
a->setShortcut(QKeySequence::Cut);
menu->addAction(a);
a = actionCopy = new QAction(tr("&Copy"), this);
a->setShortcut(QKeySequence::Copy);
menu->addAction(a);
a = actionPaste = new QAction(tr("&Paste"), this);
a->setShortcut(QKeySequence::Paste);
menu->addAction(a);
actionPaste->setEnabled(!QApplication::clipboard()->text().isEmpty());
//setup fileBrowser
splitter = new QSplitter;
//dirmodel = new QDirModel;
fileBrowser = new FileBrowser(splitter);
//fileBrowser->setModel(dirmodel);
//fileBrowser->setRootIndex(dirmodel->index(QDir::currentPath()+tr("\\..")));
//fileBrowser->setMovement(QListView::Snap);
connect(fileBrowser->view, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
SLOT(loadFromBrowser(QListWidgetItem*)));
tabArea = new QTabWidget(splitter);
tabArea->setMovable(true);
//tabArea->setUsesScrollButtons(false);
tabArea->setElideMode(Qt::ElideRight);
splitter->setCollapsible(1, false);
setCentralWidget(splitter);
//Test slot!!! (write now it retracts/opens the fileBrowser)
testbool = false;
a = new QAction(tr("&Klose"), this);
a->setShortcut(Qt::CTRL + Qt::Key_K);
connect(a, SIGNAL(triggered()), SLOT(testSlot()));
menu->addAction(a);
testSlot();
//switch tab
a = new QAction(tr("&Switch"), this);
a->setShortcut(Qt::CTRL + Qt::Key_Tab);
connect(a, SIGNAL(triggered()), SLOT(switchTab()));
menu->addAction(a);
NoteBox *box = new NoteBox(tabArea);
tabArea->addTab(box,tr("*Untitled"));
connect(box->textEdit()->document(),
SIGNAL(modificationChanged(bool)), SLOT(modified(bool)));
box->textEdit()->setFocus();
//setCentralWidget(textArea); //place this in center
//QTabBar *tb = new QTabBar(textArea);
//tb->addTab(tr("tab"));
setWindowTitle(tr("Editor"));
}
void MainWindow::loadFromBrowser(QListWidgetItem *item)
{
QFileInfo fileInfo = QFileInfo((const QString&)item->data(5));
if (fileInfo.isDir())
{
fileBrowser->setDir(fileInfo.filePath());
refreshFileBrowser();
}
else
{
load(fileInfo.filePath());
}
}
void MainWindow::testSlot()
{
QList<int> list;
if (testbool)
{
list.append(0);
list.append(80);
splitter->setSizes(list);
testbool = false;
}
else
{
list.append(50);
list.append(50);
splitter->setSizes(list);
testbool = true;
}
}
void MainWindow::fileNew() //slot
{
NoteBox *box = new NoteBox(tabArea);
QTextEdit *edit = box->textEdit();
tabArea->addTab(box,tr("*Untitled")+QString::number(++tabinc));
connect(edit->document(),SIGNAL(modificationChanged(bool)),
SLOT(modified(bool)));
tabArea->setCurrentWidget(box);
edit->setFocus();
}
void MainWindow::fileOpen() //slot
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
QString(), tr("Text Files (*.txt);;C++ Files (*.cpp *.h);;All Files (*)"));
load(fileName);
}
bool MainWindow::fileSave() //slot
{
//QFile file();
//QTextStream ts(&file);
return fileSaveAs();
}
bool MainWindow::fileSaveAs() //slot
{
QString fileName = QFileDialog::getSaveFileName(this, tr("Save as..."),
QString(), tr("Text Files (*.txt);;All Files (*)"));
if (fileName.isEmpty())
return false;
QFile file(fileName);
if (!file.open(QFile::WriteOnly))
return false;
QTextStream ts(&file);
NoteBox *current = (NoteBox*)tabArea->currentWidget();
ts << current->textEdit()->toPlainText();
current->textEdit()->document()->setModified(false);
tabArea->setTabText(tabArea->currentIndex(),QFileInfo(fileName).fileName());
current->setHighlight(fileName.endsWith(".cpp") || fileName.endsWith(".h"));
return true;
}
void MainWindow::closeTab()
{
NoteBox *current = (NoteBox*)tabArea->currentWidget();
if (tabArea->count() > 1)
{
opened.remove(current->path);
tabArea->removeTab(tabArea->currentIndex());
refreshFileBrowser();
}
else
{
int tabIndex = tabArea->currentIndex();
QString title = tabArea->tabText(tabIndex);
if(!title.startsWith("*Untitled") || title.endsWith(" +"))
{
opened.remove(current->path);
current->textEdit()->clear();
current->path = QString();
tabArea->setTabText(tabArea->currentIndex(),tr("*Untitled")+QString::number(++tabinc));
refreshFileBrowser();
}
}
}
void MainWindow::switchTab()
{
int nextIndex = tabArea->currentIndex()-1;
if (nextIndex < 0)
nextIndex = tabArea->count()-1;
tabArea->setCurrentIndex(nextIndex);
((NoteBox*)tabArea->currentWidget())->textEdit()->setFocus();
}
bool MainWindow::load(const QString &fileName)
{
NoteBox *current = (NoteBox*)tabArea->currentWidget();
if (current == 0)
{
printf("loading null widget, fail\n");
return false;
}
if (fileName.isNull())
return false;
if (!QFile::exists(fileName))
{
printf("fileName(%s) doesn't exist!\n",fileName.toAscii().constData());
return false;
}
QFile file(fileName);
if (!file.open(QFile::ReadOnly | QFile::Text))
{
printf("fileName(%s) failed to open\n",fileName.toAscii().constData());
return false;
}
if (opened.contains(fileName))
{
tabArea->setCurrentIndex(opened.value(fileName));
((NoteBox*)tabArea->currentWidget())->textEdit()->setFocus();
refreshFileBrowser();
return true;
}
QString title = tabArea->tabText(tabArea->currentIndex());
if(!title.startsWith("*Untitled") || title.endsWith(" +"))
{
//I have to reinitialize this b/c of behind the scene slot connect in Qt
NoteBox *box = new NoteBox(tabArea);
QTextEdit *edit = box->textEdit();
tabArea->addTab(box,tr("*Untitled")+QString::number(++tabinc));
connect(edit->document(),SIGNAL(modificationChanged(bool)),
SLOT(modified(bool)));
tabArea->setCurrentWidget(box);
edit->setFocus();
current = (NoteBox*)tabArea->currentWidget();
}
current->textEdit()->setPlainText(file.readAll());
current->path = fileName;
current->setHighlight(fileName.endsWith(".cpp") || fileName.endsWith(".h"));
tabArea->setTabText(tabArea->currentIndex(),QFileInfo(fileName).fileName());
opened.insert(fileName,tabArea->currentIndex());
refreshFileBrowser();
return true;
}
void MainWindow::modified(bool changed)
{
int index = tabArea->currentIndex();
if (changed)
{
tabArea->setTabText(index,tabArea->tabText(index)+tr(" +"));
}
else
{
QString title = tabArea->tabText(index);
if (title.endsWith(" +"))
{
tabArea->setTabText(index,title.left(title.size()-2));
}
}
}
void MainWindow::refreshFileBrowser()
{
int count = fileBrowser->view->count();
for (int i=0; i<count; i++)
{
QListWidgetItem *item = fileBrowser->view->item(i);
if (opened.contains( (const QString&)item->data(5) ))
item->setBackground(Qt::yellow);
else
item->setBackground(Qt::white);
}
}