1 /***************************************************************************
2 * Copyright 2006-2007 Alexander Dymo <adymo@kdevelop.org> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU Library General Public License as *
6 * published by the Free Software Foundation; either version 2 of the *
7 * License, or (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU Library General Public *
15 * License along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
19 #include "mainwindow.h"
20 #include "mainwindow_p.h"
25 #include <ksharedconfig.h>
26 #include <kconfiggroup.h>
28 #include <kwindowsystem.h>
30 #include <QApplication>
31 #include <QDesktopWidget>
37 #include "controller.h"
38 #include "container.h"
42 MainWindow::MainWindow(Controller
*controller
, Qt::WindowFlags flags
)
43 : KParts::MainWindow(0, flags
), d(new MainWindowPrivate(this, controller
))
45 connect(this, SIGNAL(destroyed()), controller
, SLOT(areaReleased()));
46 connect(this, SIGNAL(areaCleared(Sublime::Area
*)), controller
, SLOT(areaReleased(Sublime::Area
*)));
48 loadGeometry(KGlobal::config()->group("Main Window"));
51 MainWindow::~MainWindow()
53 kDebug() << "destroying mainwindow";
57 void MainWindow::setArea(Area
*area
)
59 bool differentArea
= (area
!= d
->area
);
60 /* All views will be removed from dock area now. However, this does
61 not mean those are removed from area, so prevent slotDockShown
62 from recording those views as no longer shown in the area. */
63 d
->ignoreDockShown
= true;
65 if (d
->autoAreaSettingsSave
&& differentArea
)
72 d
->activateFirstVisibleView();
73 emit
areaChanged(area
);
74 d
->ignoreDockShown
= false;
79 void MainWindow::resizeEvent(QResizeEvent
* event
)
81 return KParts::MainWindow::resizeEvent(event
);
84 void MainWindow::clearArea()
86 emit
areaCleared(d
->area
);
90 QList
<View
*> MainWindow::toolDocks() const
95 Area
*Sublime::MainWindow::area() const
100 Controller
*MainWindow::controller() const
102 return d
->controller
;
105 View
*MainWindow::activeView()
107 return d
->activeView
;
110 View
*MainWindow::activeToolView()
112 return d
->activeToolView
;
115 void MainWindow::activateView(View
*view
)
117 if (!d
->viewContainers
.contains(view
))
119 d
->viewContainers
[view
]->setCurrentWidget(view
->widget());
124 void MainWindow::setActiveView(View
*view
)
126 d
->activeView
= view
;
127 if (view
&& !view
->widget()->hasFocus())
128 view
->widget()->setFocus();
129 emit
activeViewChanged(view
);
132 void Sublime::MainWindow::setActiveToolView(View
*view
)
134 d
->activeToolView
= view
;
135 emit
activeToolViewChanged(view
);
138 void MainWindow::saveSettings()
140 QString group
= "MainWindow";
142 group
+= '_' + area()->objectName();
143 KConfigGroup cg
= KGlobal::config()->group(group
);
144 /* This will try to save window size, too. But it's OK, since we
145 won't use this information when loading. */
146 saveMainWindowSettings(cg
);
150 void MainWindow::loadSettings()
152 kDebug() << "loading settings for " << (area() ? area()->objectName() : "");
153 QString group
= "MainWindow";
155 group
+= '_' + area()->objectName();
156 KConfigGroup cg
= KGlobal::config()->group(group
);
158 // What follows is copy-paste from applyMainWindowSettings. Unfortunately,
159 // we don't really want that one to try restoring window size, and we also
160 // cannot stop it from doing that in any clean way.
161 QStatusBar
* sb
= qFindChild
<KStatusBar
*>(this);
163 QString entry
= cg
.readEntry("StatusBar", "Enabled");
164 if ( entry
== "Disabled" )
170 QMenuBar
* mb
= qFindChild
<KMenuBar
*>(this);
172 QString entry
= cg
.readEntry ("MenuBar", "Enabled");
173 if ( entry
== "Disabled" )
179 if ( !autoSaveSettings() || cg
.name() == autoSaveGroup() ) {
180 QString entry
= cg
.readEntry ("ToolBarsMovable", "Enabled");
181 if ( entry
== "Disabled" )
182 KToolBar::setToolBarsLocked(true);
184 KToolBar::setToolBarsLocked(false);
187 int n
= 1; // Toolbar counter. toolbars are counted from 1,
188 foreach (KToolBar
* toolbar
, toolBars()) {
189 QString
group("Toolbar");
190 // Give a number to the toolbar, but prefer a name if there is one,
191 // because there's no real guarantee on the ordering of toolbars
192 group
+= (toolbar
->objectName().isEmpty() ? QString::number(n
) : QString(" ")+toolbar
->objectName());
194 KConfigGroup
toolbarGroup(&cg
, group
);
195 toolbar
->applySettings(toolbarGroup
, false);
199 // Utilise the QMainWindow::restoreState() functionality
200 // Note that we're fixing KMainWindow bug here -- the original
201 // code has this fragment above restoring toolbar properties.
202 // As result, each save/restore would move the toolbar a bit to
204 if (cg
.hasKey("State")) {
206 state
= cg
.readEntry("State", state
);
207 state
= QByteArray::fromBase64(state
);
208 // One day will need to load the version number, but for now, assume 0
212 KConfigGroup uiGroup
= KGlobal::config()->group("UiSettings");
213 foreach (Container
*container
, findChildren
<Container
*>())
214 container
->setTabBarHidden(uiGroup
.readEntry("TabBarVisibility", 1) == 0);
218 emit
settingsLoaded();
221 bool MainWindow::queryClose()
224 KConfigGroup
config(KGlobal::config(), "Main Window");
225 saveGeometry(config
);
228 return KParts::MainWindow::queryClose();
231 void Sublime::MainWindow::setStatusIcon(View
* view
, const QIcon
& icon
)
233 d
->setStatusIcon(view
, icon
);
236 void MainWindow::saveGeometry(KConfigGroup
&config
)
238 int scnum
= QApplication::desktop()->screenNumber(parentWidget());
239 QRect desk
= QApplication::desktop()->screenGeometry(scnum
);
241 // if the desktop is virtual then use virtual screen size
242 if (QApplication::desktop()->isVirtualDesktop())
243 desk
= QApplication::desktop()->screenGeometry(QApplication::desktop()->screen());
245 QString key
= QString::fromLatin1("Desktop %1 %2")
246 .arg(desk
.width()).arg(desk
.height());
247 config
.writeEntry(key
, geometry());
250 void MainWindow::loadGeometry(const KConfigGroup
&config
)
252 // The below code, essentially, is copy-paste from
253 // KMainWindow::restoreWindowSize. Right now, that code is buggy,
254 // as per http://permalink.gmane.org/gmane.comp.kde.devel.core/52423
255 // so we implement a less theoretically correct, but working, version
257 const int scnum
= QApplication::desktop()->screenNumber(parentWidget());
258 QRect desk
= QApplication::desktop()->screenGeometry(scnum
);
260 // if the desktop is virtual then use virtual screen size
261 if (QApplication::desktop()->isVirtualDesktop())
262 desk
= QApplication::desktop()->screenGeometry(QApplication::desktop()->screen());
264 QString key
= QString::fromLatin1("Desktop %1 %2")
265 .arg(desk
.width()).arg(desk
.height());
266 QRect g
= config
.readEntry(key
, QRect());
271 void MainWindow::enableAreaSettingsSave()
273 d
->autoAreaSettingsSave
= true;
278 #include "mainwindow.moc"