Yannick Shpilka , cleaned up
[kgraphinterface.git] / src / kgraphinterface.cpp
blob2610569f9896fcf4d5523e6fdbd3907e05240a40
2 #include "kgraphinterface.h"
4 #include <kkeydialog.h>
5 #include <kfiledialog.h>
6 #include <kconfig.h>
7 #include <kurl.h>
9 #include <kedittoolbar.h>
11 #include <kaction.h>
12 #include <kstdaction.h>
14 #include <klibloader.h>
15 #include <kmessagebox.h>
16 #include <kstatusbar.h>
17 #include <klocale.h>
19 KGraphInterface::KGraphInterface()
20 : KParts::MainWindow( 0L, "KGraphInterface" )
22 // set the shell's ui resource file
23 setXMLFile("kgraphinterface_shell.rc");
25 // then, setup our actions
26 setupActions();
28 // and a status bar
29 statusBar()->show();
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("libkgraphinterfacepart");
35 if (factory)
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 "kgraphinterface_part", "KParts::ReadWritePart" ));
42 if (m_part)
44 // tell the KParts::MainWindow that this is indeed the main widget
45 setCentralWidget(m_part->widget());
47 // and integrate the part's GUI with the shell's
48 createGUI(m_part);
51 else
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;
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();
68 KGraphInterface::~KGraphInterface()
72 void KGraphInterface::load(const KURL& url)
74 m_part->openURL( url );
77 void KGraphInterface::setupActions()
79 KStdAction::openNew(this, SLOT(fileNew()), actionCollection());
80 KStdAction::open(this, SLOT(fileOpen()), actionCollection());
82 KStdAction::quit(kapp, SLOT(quit()), actionCollection());
84 m_toolbarAction = KStdAction::showToolbar(this, SLOT(optionsShowToolbar()), actionCollection());
85 m_statusbarAction = KStdAction::showStatusbar(this, SLOT(optionsShowStatusbar()), actionCollection());
87 KStdAction::keyBindings(this, SLOT(optionsConfigureKeys()), actionCollection());
88 KStdAction::configureToolbars(this, SLOT(optionsConfigureToolbars()), actionCollection());
91 void KGraphInterface::saveProperties(KConfig* /*config*/)
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
98 void KGraphInterface::readProperties(KConfig* /*config*/)
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'
106 void KGraphInterface::fileNew()
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
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() )
118 (new KGraphInterface)->show();
123 void KGraphInterface::optionsShowToolbar()
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();
133 void KGraphInterface::optionsShowStatusbar()
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();
143 void KGraphInterface::optionsConfigureKeys()
145 KKeyDialog::configure(actionCollection());
148 void KGraphInterface::optionsConfigureToolbars()
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
160 // use the standard toolbar editor
161 KEditToolbar dlg(factory());
162 connect(&dlg, SIGNAL(newToolbarConfig()),
163 this, SLOT(applyNewToolbarConfig()));
164 dlg.exec();
167 void KGraphInterface::applyNewToolbarConfig()
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
180 void KGraphInterface::fileOpen()
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 );
188 if (url.isEmpty() == false)
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() )
196 // we open the file in this window...
197 load( url );
199 else
201 // we open the file in a new window...
202 KGraphInterface* newWin = new KGraphInterface;
203 newWin->load( url );
204 newWin->show();
209 #include "kgraphinterface.moc"