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

« back to all changes in this revision

Viewing changes to languages/cpp/app_templates/kpartapp/app.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2006-05-23 18:39:42 UTC
  • Revision ID: james.westby@ubuntu.com-20060523183942-hucifbvh68k2bwz7
Tags: upstream-3.3.2
Import upstream version 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#include "%{APPNAMELC}.h"
 
3
 
 
4
#include <kkeydialog.h>
 
5
#include <kfiledialog.h>
 
6
#include <kconfig.h>
 
7
#include <kurl.h>
 
8
 
 
9
#include <kedittoolbar.h>
 
10
 
 
11
#include <kaction.h>
 
12
#include <kstdaction.h>
 
13
 
 
14
#include <klibloader.h>
 
15
#include <kmessagebox.h>
 
16
#include <kstatusbar.h>
 
17
#include <klocale.h>
 
18
 
 
19
%{APPNAME}::%{APPNAME}()
 
20
    : KParts::MainWindow( 0L, "%{APPNAME}" )
 
21
{
 
22
    // set the shell's ui resource file
 
23
    setXMLFile("%{APPNAMELC}_shell.rc");
 
24
 
 
25
    // then, setup our actions
 
26
    setupActions();
 
27
 
 
28
    // and a status bar
 
29
    statusBar()->show();
 
30
 
 
31
    // this routine will find and load our Part.  it finds the Part by
 
32
    // name which is a bad idea usually.. but it's alright in this
 
33
    // case since our Part is made for this Shell
 
34
    KLibFactory *factory = KLibLoader::self()->factory("lib%{APPNAMELC}part");
 
35
    if (factory)
 
36
    {
 
37
        // now that the Part is loaded, we cast it to a Part to get
 
38
        // our hands on it
 
39
        m_part = static_cast<KParts::ReadWritePart *>(factory->create(this,
 
40
                                "%{APPNAMELC}_part", "KParts::ReadWritePart" ));
 
41
 
 
42
        if (m_part)
 
43
        {
 
44
            // tell the KParts::MainWindow that this is indeed the main widget
 
45
            setCentralWidget(m_part->widget());
 
46
 
 
47
            // and integrate the part's GUI with the shell's
 
48
            createGUI(m_part);
 
49
        }
 
50
    }
 
51
    else
 
52
    {
 
53
        // if we couldn't find our Part, we exit since the Shell by
 
54
        // itself can't do anything useful
 
55
        KMessageBox::error(this, i18n("Could not find our part."));
 
56
        kapp->quit();
 
57
        // we return here, cause kapp->quit() only means "exit the
 
58
        // next time we enter the event loop...
 
59
        return;
 
60
    }
 
61
 
 
62
    // apply the saved mainwindow settings, if any, and ask the mainwindow
 
63
    // to automatically save settings if changed: window size, toolbar
 
64
    // position, icon size, etc.
 
65
    setAutoSaveSettings();
 
66
}
 
67
 
 
68
%{APPNAME}::~%{APPNAME}()
 
69
{
 
70
}
 
71
 
 
72
void %{APPNAME}::load(const KURL& url)
 
73
{
 
74
    m_part->openURL( url );
 
75
}
 
76
 
 
77
void %{APPNAME}::setupActions()
 
78
{
 
79
    KStdAction::openNew(this, SLOT(fileNew()), actionCollection());
 
80
    KStdAction::open(this, SLOT(fileOpen()), actionCollection());
 
81
 
 
82
    KStdAction::quit(kapp, SLOT(quit()), actionCollection());
 
83
 
 
84
    m_toolbarAction = KStdAction::showToolbar(this, SLOT(optionsShowToolbar()), actionCollection());
 
85
    m_statusbarAction = KStdAction::showStatusbar(this, SLOT(optionsShowStatusbar()), actionCollection());
 
86
 
 
87
    KStdAction::keyBindings(this, SLOT(optionsConfigureKeys()), actionCollection());
 
88
    KStdAction::configureToolbars(this, SLOT(optionsConfigureToolbars()), actionCollection());
 
89
}
 
90
 
 
91
void %{APPNAME}::saveProperties(KConfig* /*config*/)
 
92
{
 
93
    // the 'config' object points to the session managed
 
94
    // config file.  anything you write here will be available
 
95
    // later when this app is restored
 
96
}
 
97
 
 
98
void %{APPNAME}::readProperties(KConfig* /*config*/)
 
99
{
 
100
    // the 'config' object points to the session managed
 
101
    // config file.  this function is automatically called whenever
 
102
    // the app is being restored.  read in here whatever you wrote
 
103
    // in 'saveProperties'
 
104
}
 
105
 
 
106
void %{APPNAME}::fileNew()
 
107
{
 
108
    // this slot is called whenever the File->New menu is selected,
 
109
    // the New shortcut is pressed (usually CTRL+N) or the New toolbar
 
110
    // button is clicked
 
111
 
 
112
    // About this function, the style guide (
 
113
    // http://developer.kde.org/documentation/standards/kde/style/basics/index.html )
 
114
    // says that it should open a new window if the document is _not_
 
115
    // in its initial state.  This is what we do here..
 
116
    if ( ! m_part->url().isEmpty() || m_part->isModified() )
 
117
    {
 
118
        (new %{APPNAME})->show();
 
119
    };
 
120
}
 
121
 
 
122
 
 
123
void %{APPNAME}::optionsShowToolbar()
 
124
{
 
125
    // this is all very cut and paste code for showing/hiding the
 
126
    // toolbar
 
127
    if (m_toolbarAction->isChecked())
 
128
        toolBar()->show();
 
129
    else
 
130
        toolBar()->hide();
 
131
}
 
132
 
 
133
void %{APPNAME}::optionsShowStatusbar()
 
134
{
 
135
    // this is all very cut and paste code for showing/hiding the
 
136
    // statusbar
 
137
    if (m_statusbarAction->isChecked())
 
138
        statusBar()->show();
 
139
    else
 
140
        statusBar()->hide();
 
141
}
 
142
 
 
143
void %{APPNAME}::optionsConfigureKeys()
 
144
{
 
145
    KKeyDialog::configure(actionCollection());
 
146
}
 
147
 
 
148
void %{APPNAME}::optionsConfigureToolbars()
 
149
{
 
150
#if defined(KDE_MAKE_VERSION)
 
151
# if KDE_VERSION >= KDE_MAKE_VERSION(3,1,0)
 
152
    saveMainWindowSettings(KGlobal::config(), autoSaveGroup());
 
153
# else
 
154
    saveMainWindowSettings(KGlobal::config() );
 
155
# endif
 
156
#else
 
157
    saveMainWindowSettings(KGlobal::config() );
 
158
#endif
 
159
 
 
160
    // use the standard toolbar editor
 
161
    KEditToolbar dlg(factory());
 
162
    connect(&dlg, SIGNAL(newToolbarConfig()),
 
163
            this, SLOT(applyNewToolbarConfig()));
 
164
    dlg.exec();
 
165
}
 
166
 
 
167
void %{APPNAME}::applyNewToolbarConfig()
 
168
{
 
169
#if defined(KDE_MAKE_VERSION)
 
170
# if KDE_VERSION >= KDE_MAKE_VERSION(3,1,0)
 
171
    applyMainWindowSettings(KGlobal::config(), autoSaveGroup());
 
172
# else
 
173
    applyMainWindowSettings(KGlobal::config());
 
174
# endif
 
175
#else
 
176
    applyMainWindowSettings(KGlobal::config());
 
177
#endif
 
178
}
 
179
 
 
180
void %{APPNAME}::fileOpen()
 
181
{
 
182
    // this slot is called whenever the File->Open menu is selected,
 
183
    // the Open shortcut is pressed (usually CTRL+O) or the Open toolbar
 
184
    // button is clicked
 
185
    KURL url =
 
186
        KFileDialog::getOpenURL( QString::null, QString::null, this );
 
187
 
 
188
    if (url.isEmpty() == false)
 
189
    {
 
190
        // About this function, the style guide (
 
191
        // http://developer.kde.org/documentation/standards/kde/style/basics/index.html )
 
192
        // says that it should open a new window if the document is _not_
 
193
        // in its initial state.  This is what we do here..
 
194
        if ( m_part->url().isEmpty() && ! m_part->isModified() )
 
195
        {
 
196
            // we open the file in this window...
 
197
            load( url );
 
198
        }
 
199
        else
 
200
        {
 
201
            // we open the file in a new window...
 
202
            %{APPNAME}* newWin = new %{APPNAME};
 
203
            newWin->load( url );
 
204
            newWin->show();
 
205
        }
 
206
    }
 
207
}
 
208
 
 
209
#include "%{APPNAMELC}.moc"