20 #include <Qsci/qsciscintilla.h>
22 #include "NCLTextEditorMainWindow.h"
24 NCLTextEditorMainWindow::NCLTextEditorMainWindow(QWidget *parent):
25 QMainWindow(parent), searchBox(this)
32 #ifdef NCLEDITOR_STANDALONE
39 setDockOptions(NCLTextEditorMainWindow::AllowNestedDocks
40 | NCLTextEditorMainWindow::AllowTabbedDocks
41 | NCLTextEditorMainWindow::AnimatedDocks);
43 #ifndef NCLEDITOR_STANDALONE
44 this->setWindowFlags(Qt::CustomizeWindowHint);
55 connect(textEdit, SIGNAL(textChanged()),
this, SLOT(documentWasModified()));
57 #ifdef NCLEDITOR_STANDALONE
58 connect ( outlineView, SIGNAL(itemClicked(QTreeWidgetItem*,
int)),
59 SLOT(gotoLineOf(QTreeWidgetItem *,
int)) );
63 setUnifiedTitleAndToolBarOnMac(
true);
67 void NCLTextEditorMainWindow::closeEvent(QCloseEvent *event)
77 void NCLTextEditorMainWindow::newFile()
85 void NCLTextEditorMainWindow::open()
88 QString fileName = QFileDialog::getOpenFileName(
this);
89 if (!fileName.isEmpty())
94 bool NCLTextEditorMainWindow::save()
96 if (curFile.isEmpty()) {
99 return saveFile(curFile);
103 bool NCLTextEditorMainWindow::saveAs()
105 QString fileName = QFileDialog::getSaveFileName(
this);
106 if (fileName.isEmpty())
109 return saveFile(fileName);
112 void NCLTextEditorMainWindow::about()
114 QMessageBox::about(
this, tr(
"About Application"),
115 tr(
"The <b>Application</b> example demonstrates how to "
116 "write modern GUI applications using Qt, with a menu "
117 "bar, toolbars, and a status bar."));
120 void NCLTextEditorMainWindow::documentWasModified()
122 setWindowModified(textEdit->isModified());
125 void NCLTextEditorMainWindow::createActions()
127 newAct =
new QAction(QIcon(
":/images/new.png"), tr(
"&New"),
this);
128 newAct->setShortcut(tr(
"Ctrl+N"));
129 newAct->setStatusTip(tr(
"Create a new file"));
130 connect(newAct, SIGNAL(triggered()),
this, SLOT(newFile()));
132 openAct =
new QAction(QIcon(
":/images/open.png"), tr(
"&Open..."),
this);
133 openAct->setShortcut(tr(
"Ctrl+O"));
134 openAct->setStatusTip(tr(
"Open an existing file"));
135 connect(openAct, SIGNAL(triggered()),
this, SLOT(open()));
137 saveAct =
new QAction(QIcon(
":/images/save.png"), tr(
"&Save"),
this);
138 saveAct->setShortcut(tr(
"Ctrl+S"));
139 saveAct->setStatusTip(tr(
"Save the document to disk"));
140 connect(saveAct, SIGNAL(triggered()),
this, SLOT(save()));
142 saveAsAct =
new QAction(tr(
"Save &As..."),
this);
143 saveAsAct->setStatusTip(tr(
"Save the document under a new name"));
144 connect(saveAsAct, SIGNAL(triggered()),
this, SLOT(saveAs()));
146 exitAct =
new QAction(tr(
"E&xit"),
this);
147 exitAct->setShortcut(tr(
"Ctrl+Q"));
148 exitAct->setStatusTip(tr(
"Exit the application"));
149 connect(exitAct, SIGNAL(triggered()),
this, SLOT(close()));
151 cutAct =
new QAction(QIcon(
":/images/clipboard_cut.png"), tr(
"Cu&t"),
this);
152 cutAct->setShortcut(tr(
"Ctrl+X"));
153 cutAct->setStatusTip(tr(
"Cut the current selection's contents to the "
155 connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut()));
157 copyAct =
new QAction(QIcon(
":/images/copy.png"), tr(
"&Copy"),
this);
158 copyAct->setShortcut(tr(
"Ctrl+C"));
159 copyAct->setStatusTip(tr(
"Copy the current selection's contents to the "
161 connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy()));
163 pasteAct =
new QAction(QIcon(
":/images/paste.png"), tr(
"&Paste"),
this);
164 pasteAct->setShortcut(tr(
"Ctrl+V"));
165 pasteAct->setStatusTip(tr(
"Paste the clipboard's contents into the current "
167 connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste()));
169 aboutAct =
new QAction(tr(
"&About"),
this);
170 aboutAct->setStatusTip(tr(
"Show the application's About box"));
171 connect(aboutAct, SIGNAL(triggered()),
this, SLOT(about()));
173 aboutQtAct =
new QAction(tr(
"About &Qt"),
this);
174 aboutQtAct->setStatusTip(tr(
"Show the Qt library's About box"));
175 connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
177 cutAct->setEnabled(
false);
178 copyAct->setEnabled(
false);
179 connect(textEdit, SIGNAL(copyAvailable(
bool)),
180 cutAct, SLOT(setEnabled(
bool)));
181 connect(textEdit, SIGNAL(copyAvailable(
bool)),
182 copyAct, SLOT(setEnabled(
bool)));
184 fullscreenAct =
new QAction( QIcon(
":/images/window_fullscreen.png"),
185 tr(
"&FullScreen"),
this);
186 fullscreenAct->setShortcut(tr(
"F11"));
187 fullscreenAct->setStatusTip(tr(
"Enable/disable fullscreen mode"));
188 connect(fullscreenAct, SIGNAL(triggered()),
this, SLOT(showInFullScreen()));
190 editPreferencesAct =
new QAction(tr(
"&Preferences"),
this);
191 editPreferencesAct->setStatusTip(tr(
"Edit Preferences related to "
193 connect ( editPreferencesAct, SIGNAL(triggered()),
194 this, SLOT(showPreferences()));
196 synchronizeAct =
new QAction ( QIcon(
":/images/synchronize-icon-24.png"),
197 tr(
"&Synchronize"),
this);
198 synchronizeAct->setStatusTip(tr(
"Synchronize current text with the others"
201 showSearchBoxAct =
new QAction(QIcon(), tr(
"Search"), textEdit);
202 showSearchBoxAct->setShortcut(tr(
"Ctrl+F"));
203 showSearchBoxAct->setShortcutContext(Qt::WindowShortcut);
204 connect(showSearchBoxAct, SIGNAL(triggered()),
this, SLOT(showSearchBox()));
206 addAction(showSearchBoxAct);
209 void NCLTextEditorMainWindow::createMenus()
211 fileMenu = menuBar()->addMenu(tr(
"&File"));
212 fileMenu->addAction(newAct);
213 fileMenu->addAction(openAct);
214 fileMenu->addAction(saveAct);
215 fileMenu->addAction(saveAsAct);
216 fileMenu->addSeparator();
217 fileMenu->addAction(exitAct);
219 editMenu = menuBar()->addMenu(tr(
"&Edit"));
220 editMenu->addAction(cutAct);
221 editMenu->addAction(copyAct);
222 editMenu->addAction(pasteAct);
223 editMenu->addAction(showSearchBoxAct);
225 editMenu->addSeparator();
226 editMenu->addAction(editPreferencesAct);
228 menuBar()->addSeparator();
230 helpMenu = menuBar()->addMenu(tr(
"&Help"));
231 helpMenu->addAction(aboutAct);
232 helpMenu->addAction(aboutQtAct);
235 void NCLTextEditorMainWindow::createToolBars()
237 fileToolBar = addToolBar(tr(
"File"));
238 fileToolBar->setObjectName(QString(
"fileToolBar"));
239 fileToolBar->addAction(newAct);
240 fileToolBar->addAction(openAct);
241 fileToolBar->addAction(saveAct);
242 fileToolBar->addAction(fullscreenAct);
244 editToolBar = addToolBar(tr(
"Edit"));
245 editToolBar->setObjectName(QString(
"editToolBar"));
246 editToolBar->addAction(cutAct);
247 editToolBar->addAction(copyAct);
248 editToolBar->addAction(pasteAct);
249 editToolBar->addAction(synchronizeAct);
252 void NCLTextEditorMainWindow::createStatusBar()
254 statusBar()->showMessage(tr(
"Ready"));
257 void NCLTextEditorMainWindow::createOutlineView()
259 #ifdef NCLEDITOR_STANDALONE
262 outlineView->setColumnCount(4);
265 dockOutlineView =
new QDockWidget(
"Outline",
this);
266 dockOutlineView->setObjectName(QString(
"dockOutlineView"));
267 dockOutlineView->setFeatures(QDockWidget::DockWidgetMovable
271 dockOutlineView->setWidget(outlineView);
273 addDockWidget(Qt::LeftDockWidgetArea, dockOutlineView);
275 outlineView->setContextMenuPolicy(Qt::ActionsContextMenu);
277 nodeMenu =
new QMenu(outlineView);
278 insertNodeChildAct =
new QAction( QIcon(
":/images/save.png"),
279 tr(
"&Add child"),
this);
280 connect ( insertNodeChildAct, SIGNAL(triggered()),
281 this, SLOT(insertElement()));
283 outlineView->addAction(insertNodeChildAct);
285 connect( outlineView,
286 SIGNAL(parserErrorNotify(QString, QString,
int,
int,
int)),
287 textEdit, SLOT(markError(QString, QString,
int,
int,
int)) );
291 void NCLTextEditorMainWindow::createProblemsView()
294 problemsView->setObjectName(QString(
"dockProblemsView"));
296 addDockWidget(Qt::RightDockWidgetArea, problemsView);
298 #ifdef NCLEDITOR_STANDALONE
299 connect( outlineView,
300 SIGNAL(parserErrorNotify(QString, QString,
int,
int,
int)),
302 SLOT(addProblem(QString, QString,
int,
int,
int)));
306 void NCLTextEditorMainWindow::createSearchBox()
308 dockSearchBox =
new QDockWidget(
"Search",
this);
309 dockSearchBox->setVisible(
false);
310 dockSearchBox->setMaximumHeight(65);
311 dockSearchBox->setObjectName(QString(
"dockSearchBox"));
312 dockSearchBox->setFeatures(QDockWidget::DockWidgetClosable );
315 searchBox.setWindowTitle(tr(
"Search..."));
317 QGridLayout *layout =
new QGridLayout(&searchBox);
318 layout->addWidget(&searchBoxText, 0, 0);
320 searchBox.setLayout(layout);
321 layout->setMargin(0);
323 QPushButton *nextButton =
new QPushButton(QIcon(
":/images/next_icon.png"),
"");
324 nextButton->setFlat(
true);
325 connect(nextButton, SIGNAL(pressed()), SLOT(findNext()));
327 QPushButton *previousButton =
new QPushButton(QIcon(
":/images/previous_icon.png"),
"");
328 previousButton->setFlat(
true);
329 connect(previousButton, SIGNAL(pressed()), SLOT(findPrevious()));
331 nextButton->setMaximumSize(16, 16);
332 previousButton->setMaximumSize(16, 16);
333 layout->addWidget(previousButton, 0, 1);
334 layout->addWidget(nextButton, 0, 2);
337 connect(&searchBoxText, SIGNAL(textChanged(QString)),
338 SLOT(findNext(QString)));
340 connect(&searchBoxText, SIGNAL(returnPressed()), SLOT(findNext()));
342 connect(&searchBoxText, SIGNAL(shiftReturnPressed()), SLOT(findPrevious()));
344 connect(&searchBoxText, SIGNAL(escPressed()), SLOT(hideSearchBox()));
346 dockSearchBox->setWidget(&searchBox);
347 addDockWidget(Qt::RightDockWidgetArea, dockSearchBox);
350 void NCLTextEditorMainWindow::readSettings()
352 QSettings settings(QSettings::IniFormat, QSettings::UserScope,
"Trolltech",
"Application Example");
353 QPoint pos = settings.value(
"pos", QPoint(200, 200)).toPoint();
354 QSize size = settings.value(
"size", QSize(400, 400)).toSize();
355 bool fullscreen = settings.value(
"fullscreen",
true).toBool();
360 restoreState(settings.value(
"qDocksState").toByteArray());
367 void NCLTextEditorMainWindow::writeSettings()
369 QSettings settings(QSettings::IniFormat, QSettings::UserScope,
"Trolltech",
"Application Example");
370 settings.setValue(
"pos", pos());
371 settings.setValue(
"size", size());
372 settings.setValue(
"fullscreen", isFullScreen());
375 settings.setValue(
"qDocksState", saveState());
378 bool NCLTextEditorMainWindow::maybeSave()
380 if (textEdit->isModified()) {
381 int ret = QMessageBox::warning(
this, tr(
"Application"),
382 tr(
"The document has been modified.\n"
383 "Do you want to save your changes?"),
384 QMessageBox::Yes | QMessageBox::Default,
386 QMessageBox::Cancel|QMessageBox::Escape);
387 if (ret == QMessageBox::Yes)
389 else if (ret == QMessageBox::Cancel)
395 void NCLTextEditorMainWindow::loadFile(
const QString &fileName)
397 QFile file(fileName);
398 if (!file.open(QFile::ReadOnly)) {
399 QMessageBox::warning(
this, tr(
"Application"),
400 tr(
"Cannot read file %1:\n%2.")
402 .arg(file.errorString()));
406 QTextStream in(&file);
407 QApplication::setOverrideCursor(Qt::WaitCursor);
408 textEdit->setText(in.readAll());
409 QApplication::restoreOverrideCursor();
411 setCurrentFile(fileName);
412 statusBar()->showMessage(tr(
"File loaded"), 2000);
415 bool NCLTextEditorMainWindow::saveFile(
const QString &fileName)
417 QFile file(fileName);
418 if (!file.open(QFile::WriteOnly)) {
419 QMessageBox::warning(
this, tr(
"Application"),
420 tr(
"Cannot write file %1:\n%2.")
422 .arg(file.errorString()));
426 QTextStream out(&file);
427 QApplication::setOverrideCursor(Qt::WaitCursor);
428 out << textEdit->text();
429 QApplication::restoreOverrideCursor();
431 setCurrentFile(fileName);
432 statusBar()->showMessage(tr(
"File saved"), 2000);
433 #ifdef NCLEDITOR_STANDALONE
434 outlineView->updateFromText(textEdit->text());
439 void NCLTextEditorMainWindow::setCurrentFile(
const QString &fileName)
442 textEdit->setModified(
false);
443 setWindowModified(
false);
446 if (curFile.isEmpty())
447 shownName =
"untitled.txt";
449 shownName = strippedName(curFile);
451 setWindowTitle(tr(
"%1[*] - %2").arg(shownName).arg(tr(
"Application")));
453 #ifdef NCLEDITOR_STANDALONE
454 outlineView->updateFromText(textEdit->text());
458 QString NCLTextEditorMainWindow::strippedName(
const QString &fullFileName)
460 return QFileInfo(fullFileName).fileName();
463 void NCLTextEditorMainWindow::showInFullScreen(){
470 void NCLTextEditorMainWindow::gotoLineOf(QTreeWidgetItem *item,
int column)
475 int line = item->text(2).toInt(&ok, 10);
476 textEdit->setCursorPosition(line-1, 0);
477 textEdit->ensureLineVisible(line-1);
478 textEdit->SendScintilla(QsciScintilla::SCI_SETFOCUS,
true);
482 void NCLTextEditorMainWindow::insertElement()
484 #ifdef NCLEDITOR_STANDALONE
486 QList<QTreeWidgetItem*> selecteds = outlineView->selectedItems ();
487 QTreeWidgetItem *item = selecteds.at (0);
488 QString
id = item->text(1);
489 int line = item->text(2).toInt ( &ok, 10 );
490 QString tagname = item->text(0);
491 QMap <QString, QString> empty;
494 map <QString, char> * children =
495 NCLStructure::getInstance()->getChildren(tagname);
497 if(children != NULL) {
498 map <QString, char>::iterator it;
499 for(it = children->begin(); it != children->end(); ++it){
500 strlist << it->first;
504 QString element = QInputDialog::getItem(
this,
512 if(ok && !element.isEmpty())
515 outlineView->addElement(item, 0, element, QString(
""), empty, line, 0);
518 int endLine = textEdit
519 ->SendScintilla( QsciScintilla::SCI_GETLINEENDPOSITION,
521 int beginLine = textEdit
522 ->SendScintilla(QsciScintilla::SCI_POSITIONFROMLINE,
524 int end_element_column = item->text(3).toInt(&ok, 10);
525 bool fix_next_line_indentation =
false;
528 char ch = textEdit->SendScintilla(
529 QsciScintilla::SCI_GETCHARAT, beginLine +
530 end_element_column-1);
532 QString endtag =
">";
534 textEdit->insertAt(tagname, line-1, end_element_column);
535 textEdit->insertAt(endtag, line-1, end_element_column-1);
540 map <QString, bool> *attributes =
541 NCLStructure::getInstance()->getAttributes(element);
542 if(attributes != NULL) {
543 map <QString, bool>::iterator it;
544 for(it = attributes->begin(); it != attributes->end(); ++it){
547 element += it->first +
"=\"\"";
552 element.prepend(
"<");
553 element.append(
"/>");
554 if(end_element_column != endLine-beginLine) {
555 fix_next_line_indentation =
true;
556 element.append(
"\n");
559 element.prepend(
"\n");
564 textEdit->insertAt(element, line-1, end_element_column);
567 int lineident = textEdit->SendScintilla(
568 QsciScintilla::SCI_GETLINEINDENTATION,
570 textEdit->SendScintilla(QsciScintilla::SCI_GETLINEINDENTATION, line-1);
572 if(fix_next_line_indentation)
573 textEdit->SendScintilla( QsciScintilla::SCI_SETLINEINDENTATION,
576 textEdit->SendScintilla(QsciScintilla::SCI_SETLINEINDENTATION,
580 textEdit->setCursorPosition(line, 0);
581 textEdit->ensureLineVisible(line);
582 textEdit->SendScintilla(QsciScintilla::SCI_SETFOCUS,
true);
584 emit elementAdded(tagname,
id, empty,
false);
590 void NCLTextEditorMainWindow::createTextView() {
591 dockTextEdit =
new QDockWidget(
"Text",
this);
592 dockTextEdit->setObjectName(QString(
"dockTextView"));
593 dockTextEdit->setFeatures(QDockWidget::DockWidgetMovable);
596 #ifndef NCLEDITOR_STANDALONE
597 QWidget* titleWidget =
new QWidget(
this);
598 dockTextEdit->setTitleBarWidget( titleWidget );
610 textEdit->setTabBehavior(NCLTextEditor::TAB_BEHAVIOR_NEXT_ATTR);
611 dockTextEdit->setWidget(textEdit);
618 addDockWidget(Qt::RightDockWidgetArea, dockTextEdit);
628 void NCLTextEditorMainWindow::showPreferences()
633 void NCLTextEditorMainWindow::showSearchBox()
635 dockSearchBox->show();
636 searchBoxText.setFocus();
639 void NCLTextEditorMainWindow::hideSearchBox()
641 dockSearchBox->hide();
642 textEdit->setFocus();
645 void NCLTextEditorMainWindow::findNext()
647 QString text = searchBoxText.text();
651 void NCLTextEditorMainWindow::findNext(QString text)
653 textEdit->findFirst(text,
true,
false,
true,
true);
656 void NCLTextEditorMainWindow::findPrevious()
658 QString text = searchBoxText.text();
662 void NCLTextEditorMainWindow::findPrevious(QString text)
665 textEdit->getCursorPosition(&line, &index);
666 index -= text.size();
667 if(index < 0) line--;
669 textEdit->findFirst(text,
true,
false,
true,
true,
false, line, index);