~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to languages/cpp/app_templates/qt4makeapp/qt4makeapp.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2010-05-05 07:21:55 UTC
  • mfrom: (1.2.3 upstream) (5.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100505072155-h78lx19pu04sbhtn
Tags: 4:4.0.0-2
* Upload to unstable (Closes: #579947, #481832).
* Acknowledge obsolete NMU fixes (Closes: #562410, #546961).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
%{CPP_TEMPLATE}
2
 
 
3
 
#include <QtGui>
4
 
#include "%{APPNAMELC}.h"
5
 
 
6
 
#include <QTextEdit>
7
 
#include <QTextStream>
8
 
#include <QCloseEvent>
9
 
#include <QFileDialog>
10
 
 
11
 
%{APPNAME}::%{APPNAME}()
12
 
{
13
 
      textEdit = new QTextEdit;
14
 
      setCentralWidget(textEdit);
15
 
      
16
 
      createActions();
17
 
      createMenus();
18
 
      createToolBars();
19
 
      createStatusBar();
20
 
      
21
 
      readSettings();
22
 
      
23
 
      connect(textEdit->document(), SIGNAL(contentsChanged()),
24
 
            this, SLOT(documentWasModified()));
25
 
      
26
 
      setCurrentFile("");
27
 
}
28
 
 
29
 
void %{APPNAME}::closeEvent(QCloseEvent *event)
30
 
{
31
 
      if (maybeSave()) {
32
 
            writeSettings();
33
 
            event->accept();
34
 
      } else {
35
 
            event->ignore();
36
 
      }
37
 
}
38
 
 
39
 
void %{APPNAME}::newFile()
40
 
{
41
 
      if (maybeSave()) {
42
 
            textEdit->clear();
43
 
            setCurrentFile("");
44
 
      }
45
 
}
46
 
 
47
 
void %{APPNAME}::open()
48
 
{
49
 
      if (maybeSave()) {
50
 
            QString fileName = QFileDialog::getOpenFileName(this);
51
 
            if (!fileName.isEmpty())
52
 
            loadFile(fileName);
53
 
      }
54
 
}
55
 
 
56
 
bool %{APPNAME}::save()
57
 
{
58
 
      if (curFile.isEmpty()) {
59
 
            return saveAs();
60
 
      } else {
61
 
            return saveFile(curFile);
62
 
      }
63
 
}
64
 
 
65
 
bool %{APPNAME}::saveAs()
66
 
{
67
 
      QString fileName = QFileDialog::getSaveFileName(this);
68
 
      if (fileName.isEmpty())
69
 
            return false;
70
 
 
71
 
      return saveFile(fileName);
72
 
}
73
 
 
74
 
void %{APPNAME}::about()
75
 
{
76
 
      QMessageBox::about(this, tr("About Application"),
77
 
            tr("The <b>Application</b> example demonstrates how to "
78
 
                  "write modern GUI applications using Qt, with a menu bar, "
79
 
                  "toolbars, and a status bar."));
80
 
}
81
 
 
82
 
void %{APPNAME}::documentWasModified()
83
 
{
84
 
      setWindowModified(true);
85
 
}
86
 
 
87
 
void %{APPNAME}::createActions()
88
 
{
89
 
      newAct = new QAction(QIcon(":/filenew.xpm"), tr("&New"), this);
90
 
      newAct->setShortcut(tr("Ctrl+N"));
91
 
      newAct->setStatusTip(tr("Create a new file"));
92
 
      connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
93
 
 
94
 
      openAct = new QAction(QIcon(":/fileopen.xpm"), tr("&Open..."), this);
95
 
      openAct->setShortcut(tr("Ctrl+O"));
96
 
      openAct->setStatusTip(tr("Open an existing file"));
97
 
      connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
98
 
 
99
 
      saveAct = new QAction(QIcon(":/filesave.xpm"), tr("&Save"), this);
100
 
      saveAct->setShortcut(tr("Ctrl+S"));
101
 
      saveAct->setStatusTip(tr("Save the document to disk"));
102
 
      connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
103
 
 
104
 
      saveAsAct = new QAction(tr("Save &As..."), this);
105
 
      saveAsAct->setStatusTip(tr("Save the document under a new name"));
106
 
      connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
107
 
 
108
 
      exitAct = new QAction(tr("E&xit"), this);
109
 
      exitAct->setShortcut(tr("Ctrl+Q"));
110
 
      exitAct->setStatusTip(tr("Exit the application"));
111
 
      connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
112
 
 
113
 
      cutAct = new QAction(QIcon(":/editcut.xpm"), tr("Cu&t"), this);
114
 
      cutAct->setShortcut(tr("Ctrl+X"));
115
 
      cutAct->setStatusTip(tr("Cut the current selection's contents to the "
116
 
                              "clipboard"));
117
 
      connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut()));
118
 
 
119
 
      copyAct = new QAction(QIcon(":/editcopy.xpm"), tr("&Copy"), this);
120
 
      copyAct->setShortcut(tr("Ctrl+C"));
121
 
      copyAct->setStatusTip(tr("Copy the current selection's contents to the "
122
 
                              "clipboard"));
123
 
      connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy()));
124
 
 
125
 
      pasteAct = new QAction(QIcon(":/editpaste.xpm"), tr("&Paste"), this);
126
 
      pasteAct->setShortcut(tr("Ctrl+V"));
127
 
      pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current "
128
 
                              "selection"));
129
 
      connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste()));
130
 
 
131
 
      aboutAct = new QAction(tr("&About"), this);
132
 
      aboutAct->setStatusTip(tr("Show the application's About box"));
133
 
      connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
134
 
 
135
 
      aboutQtAct = new QAction(tr("About &Qt"), this);
136
 
      aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
137
 
      connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
138
 
 
139
 
      cutAct->setEnabled(false);
140
 
      copyAct->setEnabled(false);
141
 
      connect(textEdit, SIGNAL(copyAvailable(bool)),
142
 
            cutAct, SLOT(setEnabled(bool)));
143
 
      connect(textEdit, SIGNAL(copyAvailable(bool)),
144
 
            copyAct, SLOT(setEnabled(bool)));
145
 
}
146
 
 
147
 
void %{APPNAME}::createMenus()
148
 
{
149
 
      fileMenu = menuBar()->addMenu(tr("&File"));
150
 
      fileMenu->addAction(newAct);
151
 
      fileMenu->addAction(openAct);
152
 
      fileMenu->addAction(saveAct);
153
 
      fileMenu->addAction(saveAsAct);
154
 
      fileMenu->addSeparator();
155
 
      fileMenu->addAction(exitAct);
156
 
 
157
 
      editMenu = menuBar()->addMenu(tr("&Edit"));
158
 
      editMenu->addAction(cutAct);
159
 
      editMenu->addAction(copyAct);
160
 
      editMenu->addAction(pasteAct);
161
 
 
162
 
      menuBar()->addSeparator();
163
 
 
164
 
      helpMenu = menuBar()->addMenu(tr("&Help"));
165
 
      helpMenu->addAction(aboutAct);
166
 
      helpMenu->addAction(aboutQtAct);
167
 
}
168
 
 
169
 
void %{APPNAME}::createToolBars()
170
 
{
171
 
      fileToolBar = addToolBar(tr("File"));
172
 
      fileToolBar->addAction(newAct);
173
 
      fileToolBar->addAction(openAct);
174
 
      fileToolBar->addAction(saveAct);
175
 
 
176
 
      editToolBar = addToolBar(tr("Edit"));
177
 
      editToolBar->addAction(cutAct);
178
 
      editToolBar->addAction(copyAct);
179
 
      editToolBar->addAction(pasteAct);
180
 
}
181
 
 
182
 
void %{APPNAME}::createStatusBar()
183
 
{
184
 
      statusBar()->showMessage(tr("Ready"));
185
 
}
186
 
 
187
 
void %{APPNAME}::readSettings()
188
 
{
189
 
      QSettings settings("Trolltech", "Application Example");
190
 
      QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
191
 
      QSize size = settings.value("size", QSize(400, 400)).toSize();
192
 
      resize(size);
193
 
      move(pos);
194
 
}
195
 
 
196
 
void %{APPNAME}::writeSettings()
197
 
{
198
 
      QSettings settings("Trolltech", "Application Example");
199
 
      settings.setValue("pos", pos());
200
 
      settings.setValue("size", size());
201
 
}
202
 
 
203
 
bool %{APPNAME}::maybeSave()
204
 
{
205
 
      if (textEdit->document()->isModified()) {
206
 
            int ret = QMessageBox::warning(this, tr("Application"),
207
 
                        tr("The document has been modified.\n"
208
 
                        "Do you want to save your changes?"),
209
 
                        QMessageBox::Yes | QMessageBox::Default,
210
 
                        QMessageBox::No,
211
 
                        QMessageBox::Cancel | QMessageBox::Escape);
212
 
            if (ret == QMessageBox::Yes)
213
 
            return save();
214
 
            else if (ret == QMessageBox::Cancel)
215
 
            return false;
216
 
      }
217
 
      return true;
218
 
}
219
 
 
220
 
void %{APPNAME}::loadFile(const QString &fileName)
221
 
{
222
 
      QFile file(fileName);
223
 
      if (!file.open(QFile::ReadOnly | QFile::Text)) {
224
 
            QMessageBox::warning(this, tr("Application"),
225
 
                              tr("Cannot read file %1:\n%2.")
226
 
                              .arg(fileName)
227
 
                              .arg(file.errorString()));
228
 
            return;
229
 
      }
230
 
 
231
 
      QTextStream in(&file);
232
 
      QApplication::setOverrideCursor(Qt::WaitCursor);
233
 
      textEdit->setPlainText(in.readAll());
234
 
      QApplication::restoreOverrideCursor();
235
 
 
236
 
      setCurrentFile(fileName);
237
 
      statusBar()->showMessage(tr("File loaded"), 2000);
238
 
}
239
 
 
240
 
bool %{APPNAME}::saveFile(const QString &fileName)
241
 
{
242
 
      QFile file(fileName);
243
 
      if (!file.open(QFile::WriteOnly | QFile::Text)) {
244
 
            QMessageBox::warning(this, tr("Application"),
245
 
                              tr("Cannot write file %1:\n%2.")
246
 
                              .arg(fileName)
247
 
                              .arg(file.errorString()));
248
 
            return false;
249
 
      }
250
 
 
251
 
      QTextStream out(&file);
252
 
      QApplication::setOverrideCursor(Qt::WaitCursor);
253
 
      out << textEdit->toPlainText();
254
 
      QApplication::restoreOverrideCursor();
255
 
 
256
 
      setCurrentFile(fileName);
257
 
      statusBar()->showMessage(tr("File saved"), 2000);
258
 
      return true;
259
 
}
260
 
 
261
 
void %{APPNAME}::setCurrentFile(const QString &fileName)
262
 
{
263
 
      curFile = fileName;
264
 
      textEdit->document()->setModified(false);
265
 
      setWindowModified(false);
266
 
 
267
 
      QString shownName;
268
 
      if (curFile.isEmpty())
269
 
            shownName = "untitled.txt";
270
 
      else
271
 
            shownName = strippedName(curFile);
272
 
 
273
 
      setWindowTitle(tr("%1[*] - %2").arg(shownName).arg(tr("Application")));
274
 
}
275
 
 
276
 
QString %{APPNAME}::strippedName(const QString &fullFileName)
277
 
{
278
 
      return QFileInfo(fullFileName).fileName();
279
 
}
280
 
 
281
 
%{APPNAME}::~%{APPNAME}()
282
 
{
283
 
 
284
 
}
285