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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
tabs = new QTabWidget();
watcher = new QFileSystemWatcher();
ui->mainToolBar->setFloatable(false);
ui->mainToolBar->setMovable(false);
setWindowTitle("cute edit");
setCentralWidget(tabs);
QObject::connect(watcher , SIGNAL(fileChanged(QString)) , this , SLOT(reloadfile(QString)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::savefile()
{
QString cfileurl=list[tabs->currentIndex()];
if(cfileurl.isEmpty())
{
savefileas();
}
else
{
QFile file(cfileurl);
if(file.open(QIODevice::WriteOnly|QIODevice::Text))
{
QPlainTextEdit * editor =qobject_cast<QPlainTextEdit *>(tabs->currentWidget());
file.write(editor->toPlainText().toUtf8());
statusBar()->showMessage(tr("file successfully saved") , 3000);
}
}
}
void MainWindow::savefile(QString url)
{
QFile file(url);
if(file.open(QIODevice::WriteOnly|QIODevice::Text))
{
QPlainTextEdit * editor =qobject_cast<QPlainTextEdit *>(tabs->currentWidget());
watcher->disconnect();
file.write(editor->toPlainText().toUtf8());
watcher->blockSignals(false);
list[tabs->currentIndex()]=url;
QFileInfo fileinfo(file);
registerfile(url , fileinfo.fileName() , tabs->currentIndex());
statusBar()->showMessage(tr("file succesfully saved") , 3000);
}
}
void MainWindow::savefileas()
{
QString fileurl;
fileurl=QFileDialog::getSaveFileName();
savefile(fileurl);
}
void MainWindow::openfile()
{
QString fileurl;
fileurl = QFileDialog::getOpenFileName();
QFile file(fileurl);
if(list.contains(fileurl))
{
switchtotab(fileurl);
return ;
}
if(file.open(QIODevice::ReadOnly|QIODevice::Text))
{
QPlainTextEdit * edit =qobject_cast<QPlainTextEdit *>(tabs->currentWidget());
int i;
if(!edit->toPlainText().isEmpty())
{
i = newfile();
}
else
{
i = tabs->currentIndex();
}
QPlainTextEdit * editor =qobject_cast<QPlainTextEdit *>(tabs->widget(i));
editor->setPlainText(file.readAll());
QFileInfo info(file);
registerfile(fileurl ,info.fileName() , i);
}
}
void MainWindow::on_actionSave_triggered()
{
savefile();
}
void MainWindow::on_actionCut_triggered()
{
cut();
}
void MainWindow::on_actionCopy_triggered()
{
copy();
}
void MainWindow::on_actionPaste_triggered()
{
paste();
}
void MainWindow::on_actionSelect_all_triggered()
{
selectall();
}
void MainWindow::on_actionDelete_triggered()
{
deleteselected();
}
bool MainWindow::mydiscarddocument()
{
QPlainTextEdit * editor =qobject_cast<QPlainTextEdit *>(tabs->currentWidget());
if(editor->document()->isModified())
{
QString filename=list[tabs->currentIndex()];
if(filename.isEmpty()){filename=tr("unnamed");}
if(!QMessageBox::question(this , tr("save document") , tr("you want to create a new document" "but the changes in current document has not been saved"
"how do you want to proceed") ,tr("save document") , tr("discard document")))
{
savefile();
return true;
}
}
return false;
}
void MainWindow::on_actionnew_2_triggered()
{
newfile();
}
void MainWindow::on_actionCut_2_triggered()
{
cut();
}
void MainWindow::on_actionCopy_2_triggered()
{
copy();
}
void MainWindow::on_actionQuit_triggered()
{
this->close();
}
void MainWindow::on_actionManual_triggered()
{
}
void MainWindow::manaul()
{
QMessageBox box;
box.setText("cute edit is a small text editor which is aimed to be easy in use and simple and with basic features for a home user");
}
void MainWindow::on_actionFind_and_replace_triggered()
{
fard();
}
void MainWindow::fard()
{
QWidget * win = new QWidget();
QVBoxLayout * layout = new QVBoxLayout();
find = new QLineEdit();
replace = new QLineEdit();
QLabel * tofind = new QLabel("to find");
QLabel * toreplace = new QLabel("replace with");
QPushButton * button = new QPushButton("find and replace");
layout->addWidget(tofind);
layout->addWidget(find);
layout->addWidget(toreplace);
layout->addWidget(replace);
layout->addWidget(button);
QObject::connect(button , SIGNAL(clicked()) , this , SLOT(far()));
win->setFixedSize(400 , 200);
win->setWindowTitle("find and replace");
win->setLayout(layout);
win->show();
}
void MainWindow::far()
{
QPlainTextEdit * editor =qobject_cast<QPlainTextEdit *>(tabs->currentWidget());
QString tofind = find->text();
QString toreplace = replace->text();
editor->setPlainText(editor->toPlainText().replace(tofind , toreplace));
}
void MainWindow::addtab()
{
QTextEdit * edit = new QTextEdit();
tabs->addTab(edit , "213123");
}
void MainWindow::cut()
{
QPlainTextEdit * editor =qobject_cast<QPlainTextEdit *>(tabs->currentWidget());
editor->cut();
}
void MainWindow::copy()
{
QPlainTextEdit * editor =qobject_cast<QPlainTextEdit *>(tabs->currentWidget());
editor->copy();
}
void MainWindow::paste()
{
QPlainTextEdit * editor =qobject_cast<QPlainTextEdit *>(tabs->currentWidget());
editor->paste();
}
void MainWindow::selectall()
{
QPlainTextEdit * editor =qobject_cast<QPlainTextEdit *>(tabs->currentWidget());
editor->selectAll();
}
void MainWindow::deleteselected()
{
QPlainTextEdit * editor =qobject_cast<QPlainTextEdit *>(tabs->currentWidget());
editor->textCursor().removeSelectedText();
}
int MainWindow::newfile()
{
QPlainTextEdit * edit = new QPlainTextEdit();
int i = tabs->addTab(edit , "untilted");
tabs->setCurrentIndex(i);
list.insert(i , "");
return i;
}
void MainWindow::registerfile(QString url, QString basename , int index)
{
list[index]=url;
watcher->addPath(url);
tabs->setTabText(index , basename);
}
bool MainWindow::newfile(QString url)
{
if(list.contains(url))
{
switchtotab(url);
return true;
}
QPlainTextEdit * edit = new QPlainTextEdit();
QFile file(url);
if(file.exists())
{
QFileInfo fileinfo(file);
if(file.open(QIODevice::ReadOnly|QIODevice::Text))
{
int i = tabs->addTab(edit , fileinfo.baseName());
registerfile(url , fileinfo.fileName() , i);
QPlainTextEdit * editor =qobject_cast<QPlainTextEdit *>(tabs->widget(i));
editor->setPlainText(file.readAll());
}
else
{
return false;
}
}
return true;
}
void MainWindow::on_actionFile_triggered()
{
newfile();
}
void MainWindow::fileurl()
{
QWidget * win = new QWidget();
QVBoxLayout * layout = new QVBoxLayout();
url = new QLineEdit();
QPushButton * button = new QPushButton("open");
layout->addWidget(url);
layout->addWidget(button);
win->setFixedWidth(500);
win->setLayout(layout);
win->show();
QObject::connect(button , SIGNAL(clicked()) , this , SLOT(openfileurl()));
}
void MainWindow::openfileurl()
{
QString u = url->text();
if(list.contains(u))
{
switchtotab(u);
return ;
}
QFile file(u);
if(!file.exists()){return;}
if(file.open(QIODevice::ReadOnly|QIODevice::Text))
{
int i = newfile();
QPlainTextEdit * editor = qobject_cast<QPlainTextEdit *>(tabs->widget(i));
editor->setPlainText(file.readAll());
list[i]=u;
QFileInfo info(file);
registerfile(u ,info.fileName() , i);
}
}
void MainWindow::on_actionUndo_triggered()
{
undo();
}
void MainWindow::redo()
{
QPlainTextEdit * edit = qobject_cast<QPlainTextEdit *>(tabs->currentWidget());
edit->redo();
}
void MainWindow::undo()
{
QPlainTextEdit *edit = qobject_cast<QPlainTextEdit *>(tabs->currentWidget());
edit->undo();
}
void MainWindow::on_actionRedo_triggered()
{
redo();
}
void MainWindow::on_actionUndo_2_triggered()
{
undo();
}
void MainWindow::on_actionRedo_2_triggered()
{
redo();
}
void MainWindow::on_actionFile_2_triggered()
{
openfile();
}
void MainWindow::on_actionUrl_triggered()
{
fileurl();
}
void MainWindow::switchtotab(QString url)
{
int index = list.indexOf(url);
tabs->setCurrentIndex(index);
}
void MainWindow::on_actionPaste_2_triggered()
{
paste();
}
void MainWindow::reloadfile(QString url)
{
int i = list.indexOf(url);
QFile file(url);
if(!QMessageBox::question(this , "file changed" , ("the file ") + url + ("has been changed on disk what do you want") , tr("get the new file contents") , tr("save my contenst to the file")))
{
QPlainTextEdit * redit =qobject_cast<QPlainTextEdit *>(tabs->widget(i));
if(file.open(QIODevice::ReadOnly))
{
redit->setPlainText(file.readAll());
}
}
else
{
if(file.open(QIODevice::WriteOnly))
{
savefile(url);
}
}
}
void MainWindow::on_actionAbout_triggered()
{
QMessageBox::information(this , tr("about cute edit") , tr("cute edit is a basic text editor made for home users" " it is written in c++ using qt " "<html><br /><a href='http://opendeveloper.pcriot.com/'>website</a><br /></html>"));
}
void MainWindow::on_actionAbout_us_triggered()
{
QMessageBox::information(this , tr("about us") , tr("my email:shubhammaheshwariy@gmail.com" " if you have any suggestion email me if you want to join me for development not only c++ but php and web development and others then you can contact me "));
}
|