📄 mainwindow.cpp
字号:
/******************************************************************************** Copyright (C) 2004-2006 Trolltech ASA. All rights reserved.**** This file is part of the example classes of the Qt Toolkit.**** Licensees holding a valid Qt License Agreement may use this file in** accordance with the rights, responsibilities and obligations** contained therein. Please consult your licensing agreement or** contact sales@trolltech.com if any conditions of this licensing** agreement are not clear to you.**** Further information about Qt licensing is available at:** http://www.trolltech.com/products/qt/licensing.html or by** contacting info@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include <QtGui>#include <QTabBar>#include <QTextCodec>#include "qsteeditor.h"#include "stdlib.h"#include "mainwindow.h"MainWindow::MainWindow(){ global = new QSteGlobalProper("qsteglobal.properties","../data/"); codec = QTextCodec::codecForLocale(); findCodec(); tabwidget = new QTabWidget; setCentralWidget(tabwidget); createActions(); createMenus(); createToolBars(); createStatusBar(); readSettings(); connect(tabwidget,SIGNAL(currentChanged(int)),this,SLOT(textListChange(int))); setCurrentFile(""); // textEdit->setWhitespaceVisibility(QsciScintilla::WsVisible);}void MainWindow::findCodec(){ QMap<QString, QTextCodec *> codecMap; QRegExp iso8859RegExp("ISO[- ]8859-([0-9]+).*"); foreach (int mib, QTextCodec::availableMibs()) { QTextCodec *codec = QTextCodec::codecForMib(mib); QString sortKey = codec->name().toUpper(); int rank; if (sortKey.startsWith("UTF-8")) { rank = 1; } else if (sortKey.startsWith("UTF-16")) { rank = 2; } else if (iso8859RegExp.exactMatch(sortKey)) { if (iso8859RegExp.cap(1).size() == 1) rank = 3; else rank = 4; } else { rank = 5; } sortKey.prepend(QChar('0' + rank)); codecMap.insert(sortKey, codec); } m_codeclist = codecMap.values();}void MainWindow::closeEvent(QCloseEvent *event){ if (maybeSave()) { writeSettings(); event->accept(); } else { event->ignore(); }}void MainWindow::newFile(){ QSteEditor *editor = new QSteEditor(tabwidget); editor->accept(global); tabwidget->addTab(editor,"Untitled"); textEdit = editor;}void MainWindow::textListChange(QSteEditor *editor){}void MainWindow::textListChange(int index){}void MainWindow::textListRemove(QSteEditor *editro){}void MainWindow::textListRemove(const QString &text){}void MainWindow::open(){ if (maybeSave()) { QString fileName = QFileDialog::getOpenFileName(this); if (!fileName.isEmpty()) loadFile(fileName); }}void MainWindow::closeFile(){ textEdit = static_cast<QSteEditor*>(tabwidget->currentWidget()); if(textEdit == NULL) return; if (textEdit->isModified()) { int ret = QMessageBox::warning(this, tr("Application"), tr("The document has been modified.\n" "Do you want to save your changes?"), QMessageBox::Yes | QMessageBox::Default, QMessageBox::No, QMessageBox::Cancel | QMessageBox::Escape); if (ret == QMessageBox::Yes) save(); else if (ret == QMessageBox::Cancel) return ; } tabwidget->removeTab(tabwidget->currentIndex()); return ;}bool MainWindow::save(){ textEdit = static_cast<QSteEditor*>(tabwidget->currentWidget()); if(textEdit == NULL) return false; if (curFile.isEmpty()) { return saveAs(); } else { // return saveFile(curFile); return textEdit->saveFile(curFile); }}void MainWindow::copy(){ textEdit = static_cast<QSteEditor*>(tabwidget->currentWidget()); if(textEdit == NULL) return; textEdit->copy();}void MainWindow::cut(){ textEdit = static_cast<QSteEditor*>(tabwidget->currentWidget()); if(textEdit == NULL) return; textEdit->cut();}void MainWindow::paste(){ textEdit = static_cast<QSteEditor*>(tabwidget->currentWidget()); if(textEdit == NULL) return; textEdit->paste();}void MainWindow::undo(){ textEdit = static_cast<QSteEditor*>(tabwidget->currentWidget()); if(textEdit == NULL) return; textEdit->undo();}void MainWindow::redo(){ textEdit = static_cast<QSteEditor*>(tabwidget->currentWidget()); if(textEdit == NULL) return; textEdit->redo();}void MainWindow::deletetext(){ textEdit = static_cast<QSteEditor*>(tabwidget->currentWidget()); if(textEdit == NULL) return; textEdit->deleteText();}void MainWindow::selectall(){ textEdit = static_cast<QSteEditor*>(tabwidget->currentWidget()); if(textEdit == NULL) return; textEdit->selectAll();}void MainWindow::inserttab(){ textEdit = static_cast<QSteEditor*>(tabwidget->currentWidget()); if(textEdit == NULL) return; textEdit->insertTab();}void MainWindow::removetab(){ textEdit = static_cast<QSteEditor*>(tabwidget->currentWidget()); if(textEdit == NULL) return; textEdit->removeTab();}void MainWindow::insertabbv(){ textEdit = static_cast<QSteEditor*>(tabwidget->currentWidget()); if(textEdit == NULL) return; textEdit->insertAbbreviation();}void MainWindow::expandabbv(){ textEdit = static_cast<QSteEditor*>(tabwidget->currentWidget()); if(textEdit == NULL) return; textEdit->expandAbbreviation();}//commentvoid MainWindow::boxcomment(){ textEdit = static_cast<QSteEditor*>(tabwidget->currentWidget()); if(textEdit == NULL) return; textEdit->boxComment();}void MainWindow::streamcomment(){ textEdit = static_cast<QSteEditor*>(tabwidget->currentWidget()); if(textEdit == NULL) return; textEdit->streamComment();}void MainWindow::blockcomment(){ textEdit = static_cast<QSteEditor*>(tabwidget->currentWidget()); if(textEdit == NULL) return; textEdit->blockComment();}//matchvoid MainWindow::gotomatch(){ textEdit = static_cast<QSteEditor*>(tabwidget->currentWidget()); if(textEdit == NULL) return; textEdit->gotoBraceMatch();}void MainWindow::selectmatch(){ textEdit = static_cast<QSteEditor*>(tabwidget->currentWidget()); if(textEdit == NULL) return; textEdit->selectBraceMatch();}//find & replacevoid MainWindow::findorreplace(){ textEdit = static_cast<QSteEditor*>(tabwidget->currentWidget()); if(textEdit == NULL) return; FindDialog *dialog = new FindDialog(this); connect(dialog,SIGNAL(findStr(string)),this,SLOT(find(string))); connect(dialog,SIGNAL(replaceStr(string)),this,SLOT(replace(string))); dialog->exec();}int MainWindow::find(string str){ textEdit = static_cast<QSteEditor*>(tabwidget->currentWidget()); if(textEdit == NULL) return -1; textEdit->doFind(str.c_str());}int MainWindow::replace(string str){ textEdit = static_cast<QSteEditor*>(tabwidget->currentWidget()); if(textEdit == NULL) return -1; printf("str = %s\n",str.c_str()); textEdit->doReplace(str.c_str());}//printvoid MainWindow::print(){ textEdit = static_cast<QSteEditor*>(tabwidget->currentWidget()); if(textEdit == NULL) return;#ifndef QT_NO_PRINTER QPrinter printer(QPrinter::HighResolution); printer.setFullPage(true); QPrintDialog *dlg = new QPrintDialog(&printer, this); dlg->setWindowTitle(tr("Print Document")); if (dlg->exec() == QDialog::Accepted) { printf("from page = %d\n",dlg->fromPage()); printf("page copies = %d\n",printer.numCopies()); printf("page orders= %d\n",printer.pageOrder()); textEdit->filePrint(&printer); } delete dlg;#endif}void MainWindow::printview(){ textEdit = static_cast<QSteEditor*>(tabwidget->currentWidget()); if(textEdit == NULL) return;#ifndef QT_NO_PRINTER QPrinter *printer = new QPrinter(QPrinter::HighResolution); printer->setFullPage(true);#endif //move to qt-4.4.0 QPrintPreviewDialog}//margin & showlinevoid MainWindow::showline(){ textEdit = static_cast<QSteEditor*>(tabwidget->currentWidget()); if(textEdit == NULL) return; textEdit->showLineNum(true);}//fold & envolevoid MainWindow::showfoldmargin(){ textEdit = static_cast<QSteEditor*>(tabwidget->currentWidget()); if(textEdit == NULL) return; textEdit->showFoldMargin(true);}void MainWindow::foldall(){ textEdit = static_cast<QSteEditor*>(tabwidget->currentWidget()); if(textEdit == NULL) return; textEdit->foldAll();}// realodvoid MainWindow::reload(){ printf("reload\n"); QAction *action = qobject_cast<QAction *>(sender()); QByteArray codecName = action->data().toByteArray(); codec = QTextCodec::codecForName(codecName); textEdit = static_cast<QSteEditor*>(tabwidget->currentWidget()); if(textEdit == NULL) return; QString filename = textEdit->getFileName(); if(filename.length() == 0) return; QFile file(filename); if (!file.open(QFile::ReadOnly)) { QMessageBox::warning(this, ("Application"), tr("Cannot read file %1:\n%2.") .arg(filename) .arg(file.errorString())); return; } QTextStream in(&file); in.setCodec(codecName); textEdit->setText(in.readAll());}bool MainWindow::saveAs(){ QString fileName = QFileDialog::getSaveFileName(this); if (fileName.isEmpty()) return false; return saveFile(fileName);}void MainWindow::about(){ QMessageBox::about(this, tr("About Application"), tr("The <b>Application</b> example demonstrates how to " "write modern GUI applications using Qt, with a menu bar, " "toolbars, and a status bar."));}void MainWindow::documentWasModified(){ // setWindowModified(textEdit->isModified());}void MainWindow::createActions(){ newAct = new QAction(QIcon(":/images/new.png"), "&New", this); newAct->setShortcut(tr("Ctrl+N")); newAct->setStatusTip(("Create a new file")); connect(newAct, SIGNAL(triggered()), this, SLOT(newFile())); openAct = new QAction(QIcon(":/images/open.png"), "&Open...", this); openAct->setShortcut(tr("Ctrl+O")); openAct->setStatusTip(("Open an existing file")); connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -