source: tspsg-svn/trunk/src/3rdparty/qttoolbardialog-2.2_1-opensource/examples/simple/mainwindow.cpp

Last change on this file was 117, checked in by laleppa, 15 years ago

+ Started adding a toolbar customization with the ude of QtToolbarDialog? (http://qt.nokia.com/products/appdev/add-on-products/catalog/4/Widgets/qttoolbardialog/).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id URL
File size: 15.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of a Qt Solutions component.
8**
9** Commercial Usage 
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Solutions Commercial License Agreement provided
12** with the Software or, alternatively, in accordance with the terms
13** contained in a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file.  Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.1, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file.  Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** Please note Third Party Software included with Qt Solutions may impose
37** additional restrictions and it is the user's responsibility to ensure
38** that they have met the licensing requirements of the GPL, LGPL, or Qt
39** Solutions Commercial license and the relevant license of the Third
40** Party Software they are using.
41**
42** If you are unsure which license is appropriate for your use, please
43** contact Nokia at [email protected].
44**
45****************************************************************************/
46
47#include <QtGui>
48
49#include "mainwindow.h"
50#include "qttoolbardialog.h"
51
52MainWindow::MainWindow()
53{
54    textEdit = new QTextEdit;
55    setCentralWidget(textEdit);
56
57    findWidget = new QLineEdit;
58    findWidget->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
59    connect(findWidget, SIGNAL(returnPressed()), this, SLOT(find()));
60
61    createActions();
62    createMenus();
63    createToolBars();
64    createStatusBar();
65    createToolBarManager();
66
67    readSettings();
68
69    connect(textEdit->document(), SIGNAL(contentsChanged()),
70            this, SLOT(documentWasModified()));
71
72    setCurrentFile("");
73}
74
75void MainWindow::closeEvent(QCloseEvent *event)
76{
77    if (maybeSave()) {
78        writeSettings();
79        event->accept();
80    } else {
81        event->ignore();
82    }
83}
84
85void MainWindow::newFile()
86{
87    if (maybeSave()) {
88        textEdit->clear();
89        setCurrentFile("");
90    }
91}
92
93void MainWindow::open()
94{
95    if (maybeSave()) {
96        QString fileName = QFileDialog::getOpenFileName(this);
97        if (!fileName.isEmpty())
98            loadFile(fileName);
99    }
100}
101
102bool MainWindow::save()
103{
104    if (curFile.isEmpty()) {
105        return saveAs();
106    } else {
107        return saveFile(curFile);
108    }
109}
110
111bool MainWindow::saveAs()
112{
113    QString fileName = QFileDialog::getSaveFileName(this);
114    if (fileName.isEmpty())
115        return false;
116
117    return saveFile(fileName);
118}
119
120void MainWindow::configureToolBars()
121{
122    QtToolBarDialog dlg(this);
123    dlg.setToolBarManager(toolBarManager);
124    dlg.exec();
125}
126
127void MainWindow::saveToolBars()
128{
129    QString fileName = QFileDialog::getSaveFileName(this,
130            tr("Save Toolbars' State"), QString(), "*.state");
131    if (fileName.isEmpty())
132        return;
133    QFileInfo fi(fileName);
134    if (fi.suffix() != QString("state"))
135        fileName += QString(".state");
136
137    QFile file(fileName);
138    if (file.open(QFile::WriteOnly)) {
139        QByteArray array = toolBarManager->saveState();
140        file.write(array);
141        file.close();
142    }
143}
144
145void MainWindow::restoreToolBars()
146{
147    QString fileName = QFileDialog::getOpenFileName(this,
148            tr("Restore Toolbars' State"), QString(), "*.state");
149    if (fileName.isEmpty())
150        return;
151
152    QFile file(fileName);
153    if (file.open(QFile::ReadOnly)) {
154        QByteArray array = file.readAll();
155        file.close();
156        toolBarManager->restoreState(array);
157    }
158}
159
160void MainWindow::about()
161{
162   QMessageBox::about(this, tr("About Application"),
163            tr("The <b>Application</b> example demonstrates how to "
164               "write modern GUI applications using Qt, with a menu bar, "
165               "toolbars, and a status bar."));
166}
167
168void MainWindow::documentWasModified()
169{
170    setWindowModified(textEdit->document()->isModified());
171}
172
173void MainWindow::find()
174{
175    QString text = findWidget->text();
176    if (!textEdit->find(text)) {
177        statusBar()->showMessage(tr("Search hit bottom, continuing from top"), 2000);
178        QTextCursor oldCursor = textEdit->textCursor();
179        int vpos = textEdit->verticalScrollBar()->value();
180        int hpos = textEdit->horizontalScrollBar()->value();
181        QTextCursor newCursor = oldCursor;
182        newCursor.setPosition(0);
183        textEdit->setTextCursor(newCursor);
184        if (!textEdit->find(text)) {
185            statusBar()->showMessage(tr("Pattern '%1' not found").arg(text), 2000);
186            textEdit->setTextCursor(oldCursor);
187            textEdit->verticalScrollBar()->setValue(vpos);
188            textEdit->horizontalScrollBar()->setValue(hpos);
189        }
190    }
191}
192
193void MainWindow::createActions()
194{
195    findAct = 0;
196
197    newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
198    newAct->setObjectName(QString::fromUtf8("newAct"));
199    newAct->setShortcut(tr("Ctrl+N"));
200    newAct->setStatusTip(tr("Create a new file"));
201    connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
202
203    openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
204    openAct->setObjectName(QString::fromUtf8("openAct"));
205    openAct->setShortcut(tr("Ctrl+O"));
206    openAct->setStatusTip(tr("Open an existing file"));
207    connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
208
209    saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this);
210    saveAct->setObjectName(QString::fromUtf8("saveAct"));
211    saveAct->setShortcut(tr("Ctrl+S"));
212    saveAct->setStatusTip(tr("Save the document to disk"));
213    connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
214
215    saveAsAct = new QAction(tr("Save &As..."), this);
216    saveAsAct->setObjectName(QString::fromUtf8("saveAsAct"));
217    saveAsAct->setStatusTip(tr("Save the document under a new name"));
218    connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
219
220    exitAct = new QAction(tr("E&xit"), this);
221    exitAct->setObjectName(QString::fromUtf8("exitAct"));
222    exitAct->setShortcut(tr("Ctrl+Q"));
223    exitAct->setStatusTip(tr("Exit the application"));
224    connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
225
226    cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this);
227    cutAct->setObjectName(QString::fromUtf8("cutAct"));
228    cutAct->setShortcut(tr("Ctrl+X"));
229    cutAct->setStatusTip(tr("Cut the current selection's contents to the "
230                            "clipboard"));
231    connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut()));
232
233    copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this);
234    copyAct->setObjectName(QString::fromUtf8("copyAct"));
235    copyAct->setShortcut(tr("Ctrl+C"));
236    copyAct->setStatusTip(tr("Copy the current selection's contents to the "
237                             "clipboard"));
238    connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy()));
239
240    pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this);
241    pasteAct->setObjectName(QString::fromUtf8("pasteAct"));
242    pasteAct->setShortcut(tr("Ctrl+V"));
243    pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current "
244                              "selection"));
245    connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste()));
246
247    configureToolBarsAct = new QAction(tr("&Configure Toolbars..."), this);
248    configureToolBarsAct->setObjectName(QString::fromUtf8("configureToolBarsAct"));
249    configureToolBarsAct->setStatusTip(tr("Configure toolbars"));
250    QObject::connect(configureToolBarsAct, SIGNAL(triggered()),
251                this, SLOT(configureToolBars()));
252
253    saveToolBarsAct = new QAction(tr("&Save Toolbars..."), this);
254    saveToolBarsAct->setObjectName(QString::fromUtf8("saveToolBarsAct"));
255    saveToolBarsAct->setStatusTip(tr("Save toolbars' state"));
256    QObject::connect(saveToolBarsAct, SIGNAL(triggered()),
257                this, SLOT(saveToolBars()));
258
259    restoreToolBarsAct = new QAction(tr("&Restore Toolbars..."), this);
260    restoreToolBarsAct->setObjectName(QString::fromUtf8("restoreToolBarsAct"));
261    restoreToolBarsAct->setStatusTip(tr("Restore toolbars' state"));
262    QObject::connect(restoreToolBarsAct, SIGNAL(triggered()),
263                this, SLOT(restoreToolBars()));
264
265    aboutAct = new QAction(tr("&About"), this);
266    aboutAct->setObjectName(QString::fromUtf8("aboutAct"));
267    aboutAct->setStatusTip(tr("Show the application's About box"));
268    connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
269
270    aboutQtAct = new QAction(tr("About &Qt"), this);
271    aboutQtAct->setObjectName(QString::fromUtf8("aboutQtAct"));
272    aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
273    connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
274
275    cutAct->setEnabled(false);
276    copyAct->setEnabled(false);
277    connect(textEdit, SIGNAL(copyAvailable(bool)),
278            cutAct, SLOT(setEnabled(bool)));
279    connect(textEdit, SIGNAL(copyAvailable(bool)),
280            copyAct, SLOT(setEnabled(bool)));
281}
282
283void MainWindow::createMenus()
284{
285    fileMenu = menuBar()->addMenu(tr("&File"));
286    fileMenu->addAction(newAct);
287    fileMenu->addAction(openAct);
288    fileMenu->addAction(saveAct);
289    fileMenu->addAction(saveAsAct);
290    fileMenu->addSeparator();
291    fileMenu->addAction(exitAct);
292
293    editMenu = menuBar()->addMenu(tr("&Edit"));
294    editMenu->addAction(cutAct);
295    editMenu->addAction(copyAct);
296    editMenu->addAction(pasteAct);
297
298    settingsMenu = menuBar()->addMenu(tr("&Settings"));
299    settingsMenu->addAction(configureToolBarsAct);
300    settingsMenu->addAction(saveToolBarsAct);
301    settingsMenu->addAction(restoreToolBarsAct);
302
303    menuBar()->addSeparator();
304
305    helpMenu = menuBar()->addMenu(tr("&Help"));
306    helpMenu->addAction(aboutAct);
307    helpMenu->addAction(aboutQtAct);
308}
309
310void MainWindow::createToolBars()
311{
312    fileToolBar = addToolBar(tr("File"));
313    fileToolBar->setObjectName(QString::fromUtf8("fileToolBar"));
314    fileToolBar->addAction(newAct);
315    fileToolBar->addAction(openAct);
316    fileToolBar->addAction(saveAct);
317
318    editToolBar = addToolBar(tr("Edit"));
319    editToolBar->setObjectName(QString::fromUtf8("editToolBar"));
320    editToolBar->addAction(cutAct);
321    editToolBar->addAction(copyAct);
322    editToolBar->addAction(pasteAct);
323    editToolBar->addSeparator();
324
325    findAct = editToolBar->addWidget(findWidget);
326    findAct->setText(tr("Find"));
327    findAct->setObjectName(QString::fromUtf8("findAct"));
328}
329
330void MainWindow::createStatusBar()
331{
332    statusBar()->showMessage(tr("Ready"));
333}
334
335void MainWindow::createToolBarManager()
336{
337    toolBarManager = new QtToolBarManager(this);
338    toolBarManager->setMainWindow(this);
339
340    QString fileStr = tr("File");
341    QString editStr = tr("Edit");
342    QString settingsStr = tr("Settings");
343    QString helpStr = tr("Help");
344
345    toolBarManager->addToolBar(fileToolBar, fileStr);
346    toolBarManager->addToolBar(editToolBar, editStr);
347
348    toolBarManager->addAction(saveAsAct, fileStr);
349    toolBarManager->addAction(exitAct, fileStr);
350    toolBarManager->addAction(configureToolBarsAct, settingsStr);
351    toolBarManager->addAction(saveToolBarsAct, settingsStr);
352    toolBarManager->addAction(restoreToolBarsAct, settingsStr);
353    toolBarManager->addAction(aboutAct, helpStr);
354    toolBarManager->addAction(aboutQtAct, helpStr);
355}
356
357void MainWindow::readSettings()
358{
359    QSettings settings("Qt Software", "Application Example");
360    QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
361    QSize size = settings.value("size", QSize(400, 400)).toSize();
362    QByteArray toolBarsState = settings.value("toolBarsState").toByteArray();
363    QByteArray docksState = settings.value("docksState").toByteArray();
364    resize(size);
365    move(pos);
366    toolBarManager->restoreState(toolBarsState);
367    restoreState(docksState);
368}
369
370void MainWindow::writeSettings()
371{
372    QSettings settings("Qt Software", "Application Example");
373    settings.setValue("pos", pos());
374    settings.setValue("size", size());
375    settings.setValue("toolBarsState", toolBarManager->saveState());
376    settings.setValue("docksState", saveState());
377}
378
379bool MainWindow::maybeSave()
380{
381    if (textEdit->document()->isModified()) {
382        int ret = QMessageBox::warning(this, tr("Application"),
383                     tr("The document has been modified.\n"
384                        "Do you want to save your changes?"),
385                     QMessageBox::Yes | QMessageBox::Default,
386                     QMessageBox::No,
387                     QMessageBox::Cancel | QMessageBox::Escape);
388        if (ret == QMessageBox::Yes)
389            return save();
390        else if (ret == QMessageBox::Cancel)
391            return false;
392    }
393    return true;
394}
395
396void MainWindow::loadFile(const QString &fileName)
397{
398    QFile file(fileName);
399    if (!file.open(QFile::ReadOnly | QFile::Text)) {
400        QMessageBox::warning(this, tr("Application"),
401                             tr("Cannot read file %1:\n%2.")
402                             .arg(fileName)
403                             .arg(file.errorString()));
404        return;
405    }
406
407    QTextStream in(&file);
408    QApplication::setOverrideCursor(Qt::WaitCursor);
409    textEdit->setPlainText(in.readAll());
410    QApplication::restoreOverrideCursor();
411
412    setCurrentFile(fileName);
413    statusBar()->showMessage(tr("File loaded"), 2000);
414}
415
416bool MainWindow::saveFile(const QString &fileName)
417{
418    QFile file(fileName);
419    if (!file.open(QFile::WriteOnly | QFile::Text)) {
420        QMessageBox::warning(this, tr("Application"),
421                             tr("Cannot write file %1:\n%2.")
422                             .arg(fileName)
423                             .arg(file.errorString()));
424        return false;
425    }
426
427    QTextStream out(&file);
428    QApplication::setOverrideCursor(Qt::WaitCursor);
429    out << textEdit->toPlainText();
430    QApplication::restoreOverrideCursor();
431
432    setCurrentFile(fileName);
433    statusBar()->showMessage(tr("File saved"), 2000);
434    return true;
435}
436
437void MainWindow::setCurrentFile(const QString &fileName)
438{
439    curFile = fileName;
440    textEdit->document()->setModified(false);
441    setWindowModified(false);
442
443    QString shownName;
444    if (curFile.isEmpty())
445        shownName = "untitled.txt";
446    else
447        shownName = strippedName(curFile);
448
449    setWindowTitle(tr("%1[*] - %2").arg(shownName).arg(tr("Application")));
450}
451
452QString MainWindow::strippedName(const QString &fullFileName)
453{
454    return QFileInfo(fullFileName).fileName();
455}
Note: See TracBrowser for help on using the repository browser.