#include <QtGui> #include "mainwindow.h" #include "ui_mainwindow.h" #include "editor.h" MainWindow::MainWindow(QWidget *parent)     : QMainWindow(parent), ui(new Ui::MainWindow) {     ui->setupUi(this);     mdiArea = new QMdiArea;     setCentralWidget(mdiArea);     connect(mdiArea, SIGNAL(subWindowActivated(QMdiSubWindow*)),this, SLOT(updateActions()));     createActions();     createMenus();     createToolBars();     createStatusBar();     setWindowIcon(QPixmap(":/images/icon.png"));     setWindowTitle("Killua MDI Editor");     QTimer::singleShot(0,this,SLOT(loadFiles())); } MainWindow::~MainWindow() {     delete ui; }
 
 
  void MainWindow::loadFiles() {     QStringList args = QApplication::arguments();     args.removeFirst();     if (!args.isEmpty()) {         foreach(QString arg,args)             openFile(arg);         mdiArea->cascadeSubWindows();     } else {         newFile();     }     mdiArea->activateNextSubWindow(); }
 
 
  void MainWindow::newFile() {     Editor *editor = new Editor;     editor->newFile();     addEditor(editor); }
 
 
  void MainWindow::openFile(const QString &fileName) {     Editor *editor = Editor::openFile(fileName,this);     if(editor)         addEditor(editor); }
 
 
  void MainWindow::closeEvent(QCloseEvent *event) {     mdiArea->closeAllSubWindows();     if(!mdiArea->subWindowList().isEmpty()) {         event->ignore();     } else {         event->accept();     } }
 
 
  void MainWindow::open() {     Editor *editor = Editor::open(this);     if(editor)         addEditor(editor); }
 
 
  void MainWindow::save() {     if(activeEditor())         activeEditor()->save(); }
 
 
  void MainWindow::saveAs() {     if (activeEditor())         activeEditor()->saveAs(); }
 
 
  void MainWindow::cut() {     if(activeEditor())         activeEditor()->cut(); }
 
 
  void MainWindow::copy() {     if(activeEditor())         activeEditor()->copy(); }
 
 
  void MainWindow::paste() {     if(activeEditor())         activeEditor()->paste(); }
 
 
  void MainWindow::about() {     QMessageBox::about(this,tr("About Killua MDI Editor"),                        tr("Design by Killua")); }
 
 
  void MainWindow::updateActions() {     bool hasEditor = (activeEditor()!=0);     bool hasSelection = activeEditor() && activeEditor()->textCursor().hasSelection();     saveAction->setEnabled(hasEditor);     saveAsAction->setEnabled(hasEditor);     cutAction->setEnabled(hasSelection);     copyAction->setEnabled(hasSelection);     pasteAction->setEnabled(hasEditor);     closeAction->setEnabled(hasEditor);     closeAllAction->setEnabled(hasEditor);     tileAction->setEnabled(hasEditor);     cascadeAction->setEnabled(hasEditor);     nextAction->setEnabled(hasEditor);     previousAction->setEnabled(hasEditor);     separatorAction->setEnabled(hasEditor);     if(activeEditor())         activeEditor()->windowMenuAction()->setChecked(true);   }
 
 
  void MainWindow::createActions() {          newAction = new QAction(tr("&New"), this);     newAction->setIcon(QIcon(":/images/new.png"));     newAction->setShortcut(QKeySequence::New);     connect(newAction, SIGNAL(triggered()), this, SLOT(newFile()));          openAction = new QAction(tr("&Open..."), this);     openAction->setIcon(QIcon(":/images/open.png"));     openAction->setShortcut(QKeySequence::Open);     connect(openAction, SIGNAL(triggered()), this, SLOT(open()));          saveAction = new QAction(tr("&Save"), this);     saveAction->setIcon(QIcon(":/images/save.png"));     saveAction->setShortcut(QKeySequence::Save);     connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));          saveAsAction = new QAction(tr("Save &As..."), this);     connect(saveAsAction, SIGNAL(triggered()), this, SLOT(saveAs()));          exitAction = new QAction(tr("E&xit"), this);     exitAction->setShortcut(tr("Ctrl+Q"));     connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));          cutAction = new QAction(tr("Cu&t"), this);     cutAction->setIcon(QIcon(":/images/cut.png"));     cutAction->setShortcut(QKeySequence::Cut);     connect(cutAction, SIGNAL(triggered()), this, SLOT(cut()));          copyAction = new QAction(tr("&Copy"), this);     copyAction->setIcon(QIcon(":/images/copy.png"));     copyAction->setShortcut(QKeySequence::Copy);     connect(copyAction, SIGNAL(triggered()), this, SLOT(copy()));          pasteAction = new QAction(tr("&Paste"), this);     pasteAction->setIcon(QIcon(":/images/paste.png"));     pasteAction->setShortcut(QKeySequence::Paste);     connect(pasteAction, SIGNAL(triggered()), this, SLOT(paste()));          closeAction = new QAction(tr("Cl&ose"), this);     closeAction->setShortcut(QKeySequence::Close);     connect(closeAction, SIGNAL(triggered()),mdiArea, SLOT(closeActiveSubWindow()));          closeAllAction = new QAction(tr("Close &All"), this);     connect(closeAllAction, SIGNAL(triggered()), this, SLOT(close()));          tileAction = new QAction(tr("&Tile"), this);     connect(tileAction, SIGNAL(triggered()),mdiArea, SLOT(tileSubWindows()));          cascadeAction = new QAction(tr("&Cascade"), this);     connect(cascadeAction, SIGNAL(triggered()),mdiArea, SLOT(cascadeSubWindows()));          nextAction = new QAction(tr("Ne&xt"), this);     nextAction->setShortcut(QKeySequence::NextChild);     connect(nextAction, SIGNAL(triggered()),mdiArea, SLOT(activateNextSubWindow()));          previousAction = new QAction(tr("Pre&vious"), this);     previousAction->setShortcut(QKeySequence::PreviousChild);     connect(previousAction, SIGNAL(triggered()),mdiArea, SLOT(activatePreviousSubWindow()));          separatorAction = new QAction(this);     separatorAction->setSeparator(true);          aboutAction = new QAction(tr("&About"), this);     aboutAction->setStatusTip(tr("Show the application's About box"));     connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));          aboutQtAction = new QAction(tr("About &Qt"), this);     connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));     windowActionGroup = new QActionGroup(this); }
 
 
  void MainWindow::createMenus() {          fileMenu = menuBar()->addMenu("&File");     fileMenu->addAction(newAction);     fileMenu->addAction(openAction);     fileMenu->addAction(saveAction);     fileMenu->addAction(saveAsAction);     fileMenu->addSeparator();     fileMenu->addAction(exitAction);          editMenu = menuBar()->addMenu(tr("&Edit"));     editMenu->addAction(cutAction);     editMenu->addAction(copyAction);     editMenu->addAction(pasteAction);     windowMenu=menuBar()->addMenu(tr("&Windos"));     windowMenu->addAction(closeAction);     windowMenu->addAction(closeAllAction);     windowMenu->addSeparator();     windowMenu->addAction(tileAction);     windowMenu->addAction(cascadeAction);     windowMenu->addSeparator();     windowMenu->addAction(nextAction);     windowMenu->addAction(previousAction);     windowMenu->addAction(separatorAction);     menuBar()->addSeparator();     helpMenu = menuBar()->addMenu(tr("&Help"));     helpMenu->addAction(aboutAction);     helpMenu->addAction(aboutQtAction); }
 
 
  void MainWindow::createToolBars() {     fileToolBar = addToolBar(tr("FIle"));     fileToolBar->addAction(newAction);     fileToolBar->addAction(openAction);     fileToolBar->addAction(saveAction);     editToolBar = addToolBar(tr("Edit"));     editToolBar->addAction(cutAction);     editToolBar->addAction(copyAction);     editToolBar->addAction(pasteAction); }
 
 
  void MainWindow::createStatusBar() {     readyLabel = new QLabel(tr("Ready"));     statusBar()->addWidget(readyLabel,1); }
 
 
  void MainWindow::addEditor(Editor *editor) {     connect(editor,SIGNAL(copyAvailable(bool)),cutAction,SLOT(setEnabled(bool)));     connect(editor,SIGNAL(copyAvailable(bool)),copyAction,SLOT(setEnabled(bool)));     QMdiSubWindow *subWindow = mdiArea->addSubWindow(editor);     windowMenu->addAction(editor->windowMenuAction());     subWindow->show(); }
 
 
  Editor *MainWindow::activeEditor() {     QMdiSubWindow *subWindow = mdiArea->activeSubWindow();     if(subWindow)         return qobject_cast<Editor *>(subWindow->widget());     return 0; }
   |