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

« back to all changes in this revision

Viewing changes to languages/cpp/app_templates/kapp/app.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 "%{APPNAMELC}.h"
4
 
#include "pref.h"
5
 
 
6
 
#include <qdragobject.h>
7
 
#include <kprinter.h>
8
 
#include <qpainter.h>
9
 
#include <qpaintdevicemetrics.h>
10
 
 
11
 
#include <kglobal.h>
12
 
#include <klocale.h>
13
 
#include <kiconloader.h>
14
 
#include <kdeversion.h>
15
 
#include <kstatusbar.h>
16
 
#include <kaccel.h>
17
 
#include <kio/netaccess.h>
18
 
#include <kfiledialog.h>
19
 
#include <kconfig.h>
20
 
#include <kurl.h>
21
 
#include <kurldrag.h>
22
 
#include <kurlrequesterdlg.h>
23
 
 
24
 
#include <kstdaccel.h>
25
 
#include <kaction.h>
26
 
#include <kstdaction.h>
27
 
 
28
 
%{APPNAME}::%{APPNAME}()
29
 
    : KMainWindow( 0, "%{APPNAME}" ),
30
 
      m_view(new %{APPNAME}View(this)),
31
 
      m_printer(0)
32
 
{
33
 
    // accept dnd
34
 
    setAcceptDrops(true);
35
 
 
36
 
    // tell the KMainWindow that this is indeed the main widget
37
 
    setCentralWidget(m_view);
38
 
 
39
 
    // then, setup our actions
40
 
    setupActions();
41
 
 
42
 
    // and a status bar
43
 
    statusBar()->show();
44
 
 
45
 
    // Apply the create the main window and ask the mainwindow to
46
 
                // automatically save settings if changed: window size, toolbar
47
 
    // position, icon size, etc.  Also to add actions for the statusbar
48
 
                // toolbar, and keybindings if necessary.
49
 
    setupGUI();
50
 
 
51
 
    // allow the view to change the statusbar and caption
52
 
    connect(m_view, SIGNAL(signalChangeStatusbar(const QString&)),
53
 
            this,   SLOT(changeStatusbar(const QString&)));
54
 
    connect(m_view, SIGNAL(signalChangeCaption(const QString&)),
55
 
            this,   SLOT(changeCaption(const QString&)));
56
 
 
57
 
}
58
 
 
59
 
%{APPNAME}::~%{APPNAME}()
60
 
{
61
 
}
62
 
 
63
 
void %{APPNAME}::load(const KURL& url)
64
 
{
65
 
    QString target;
66
 
    // the below code is what you should normally do.  in this
67
 
    // example case, we want the url to our own.  you probably
68
 
    // want to use this code instead for your app
69
 
 
70
 
    #if 0
71
 
    // download the contents
72
 
    if (KIO::NetAccess::download(url, target))
73
 
    {
74
 
        // set our caption
75
 
        setCaption(url);
76
 
 
77
 
        // load in the file (target is always local)
78
 
        loadFile(target);
79
 
 
80
 
        // and remove the temp file
81
 
        KIO::NetAccess::removeTempFile(target);
82
 
    }
83
 
    #endif
84
 
 
85
 
    setCaption(url.prettyURL());
86
 
    m_view->openURL(url);
87
 
}
88
 
 
89
 
void %{APPNAME}::setupActions()
90
 
{
91
 
    KStdAction::openNew(this, SLOT(fileNew()), actionCollection());
92
 
    KStdAction::open(this, SLOT(fileOpen()), actionCollection());
93
 
    KStdAction::save(this, SLOT(fileSave()), actionCollection());
94
 
    KStdAction::saveAs(this, SLOT(fileSaveAs()), actionCollection());
95
 
    KStdAction::print(this, SLOT(filePrint()), actionCollection());
96
 
    KStdAction::quit(kapp, SLOT(quit()), actionCollection());
97
 
 
98
 
    KStdAction::preferences(this, SLOT(optionsPreferences()), actionCollection());
99
 
 
100
 
    // this doesn't do anything useful.  it's just here to illustrate
101
 
    // how to insert a custom menu and menu item
102
 
    KAction *custom = new KAction(i18n("Cus&tom Menuitem"), 0,
103
 
                                  this, SLOT(optionsPreferences()),
104
 
                                  actionCollection(), "custom_action");
105
 
}
106
 
 
107
 
void %{APPNAME}::saveProperties(KConfig *config)
108
 
{
109
 
    // the 'config' object points to the session managed
110
 
    // config file.  anything you write here will be available
111
 
    // later when this app is restored
112
 
 
113
 
    if (!m_view->currentURL().isEmpty()) {
114
 
#if KDE_IS_VERSION(3,1,3)
115
 
        config->writePathEntry("lastURL", m_view->currentURL());
116
 
#else
117
 
        config->writeEntry("lastURL", m_view->currentURL());
118
 
#endif
119
 
    }
120
 
}
121
 
 
122
 
void %{APPNAME}::readProperties(KConfig *config)
123
 
{
124
 
    // the 'config' object points to the session managed
125
 
    // config file.  this function is automatically called whenever
126
 
    // the app is being restored.  read in here whatever you wrote
127
 
    // in 'saveProperties'
128
 
 
129
 
    QString url = config->readPathEntry("lastURL");
130
 
 
131
 
    if (!url.isEmpty())
132
 
        m_view->openURL(KURL(url));
133
 
}
134
 
 
135
 
void %{APPNAME}::dragEnterEvent(QDragEnterEvent *event)
136
 
{
137
 
    // accept uri drops only
138
 
    event->accept(KURLDrag::canDecode(event));
139
 
}
140
 
 
141
 
void %{APPNAME}::dropEvent(QDropEvent *event)
142
 
{
143
 
    // this is a very simplistic implementation of a drop event.  we
144
 
    // will only accept a dropped URL.  the Qt dnd code can do *much*
145
 
    // much more, so please read the docs there
146
 
    KURL::List urls;
147
 
 
148
 
    // see if we can decode a URI.. if not, just ignore it
149
 
    if (KURLDrag::decode(event, urls) && !urls.isEmpty())
150
 
    {
151
 
        // okay, we have a URI.. process it
152
 
        const KURL &url = urls.first();
153
 
 
154
 
        // load in the file
155
 
        load(url);
156
 
    }
157
 
}
158
 
 
159
 
void %{APPNAME}::fileNew()
160
 
{
161
 
    // this slot is called whenever the File->New menu is selected,
162
 
    // the New shortcut is pressed (usually CTRL+N) or the New toolbar
163
 
    // button is clicked
164
 
 
165
 
    // create a new window
166
 
    (new %{APPNAME})->show();
167
 
}
168
 
 
169
 
void %{APPNAME}::fileOpen()
170
 
{
171
 
    // this slot is called whenever the File->Open menu is selected,
172
 
    // the Open shortcut is pressed (usually CTRL+O) or the Open toolbar
173
 
    // button is clicked
174
 
/*
175
 
    // this brings up the generic open dialog
176
 
    KURL url = KURLRequesterDlg::getURL(QString::null, this, i18n("Open Location") );
177
 
*/
178
 
    // standard filedialog
179
 
    KURL url = KFileDialog::getOpenURL(QString::null, QString::null, this, i18n("Open Location"));
180
 
    if (!url.isEmpty())
181
 
        m_view->openURL(url);
182
 
}
183
 
 
184
 
void %{APPNAME}::fileSave()
185
 
{
186
 
    // this slot is called whenever the File->Save menu is selected,
187
 
    // the Save shortcut is pressed (usually CTRL+S) or the Save toolbar
188
 
    // button is clicked
189
 
 
190
 
    // save the current file
191
 
}
192
 
 
193
 
void %{APPNAME}::fileSaveAs()
194
 
{
195
 
    // this slot is called whenever the File->Save As menu is selected,
196
 
    KURL file_url = KFileDialog::getSaveURL();
197
 
    if (!file_url.isEmpty() && file_url.isValid())
198
 
    {
199
 
        // save your info, here
200
 
    }
201
 
}
202
 
 
203
 
void %{APPNAME}::filePrint()
204
 
{
205
 
    // this slot is called whenever the File->Print menu is selected,
206
 
    // the Print shortcut is pressed (usually CTRL+P) or the Print toolbar
207
 
    // button is clicked
208
 
    if (!m_printer) m_printer = new KPrinter;
209
 
    if (m_printer->setup(this))
210
 
    {
211
 
        // setup the printer.  with Qt, you always "print" to a
212
 
        // QPainter.. whether the output medium is a pixmap, a screen,
213
 
        // or paper
214
 
        QPainter p;
215
 
        p.begin(m_printer);
216
 
 
217
 
        // we let our view do the actual printing
218
 
        QPaintDeviceMetrics metrics(m_printer);
219
 
        m_view->print(&p, metrics.height(), metrics.width());
220
 
 
221
 
        // and send the result to the printer
222
 
        p.end();
223
 
    }
224
 
}
225
 
 
226
 
void %{APPNAME}::optionsPreferences()
227
 
{
228
 
    // popup some sort of preference dialog, here
229
 
    %{APPNAME}Preferences dlg;
230
 
    if (dlg.exec())
231
 
    {
232
 
        // redo your settings
233
 
    }
234
 
}
235
 
 
236
 
void %{APPNAME}::changeStatusbar(const QString& text)
237
 
{
238
 
    // display the text on the statusbar
239
 
    statusBar()->message(text);
240
 
}
241
 
 
242
 
void %{APPNAME}::changeCaption(const QString& text)
243
 
{
244
 
    // display the text on the caption
245
 
    setCaption(text);
246
 
}
247
 
#include "%{APPNAMELC}.moc"