sublime
mainwindow.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "mainwindow.h"
00020 #include "mainwindow_p.h"
00021
00022 #include <kdebug.h>
00023 #include <kglobal.h>
00024 #include <kconfig.h>
00025 #include <ksharedconfig.h>
00026 #include <kconfiggroup.h>
00027 #include <ktoolbar.h>
00028 #include <kwindowsystem.h>
00029
00030 #include <QApplication>
00031 #include <QDesktopWidget>
00032 #include <KStatusBar>
00033 #include <KMenuBar>
00034
00035 #include "area.h"
00036 #include "view.h"
00037 #include "controller.h"
00038 #include "container.h"
00039
00040 namespace Sublime {
00041
00042 MainWindow::MainWindow(Controller *controller, Qt::WindowFlags flags)
00043 : KParts::MainWindow(0, flags), d(new MainWindowPrivate(this, controller))
00044 {
00045 connect(this, SIGNAL(destroyed()), controller, SLOT(areaReleased()));
00046 connect(this, SIGNAL(areaCleared(Sublime::Area*)), controller, SLOT(areaReleased(Sublime::Area*)));
00047
00048 loadGeometry(KGlobal::config()->group("Main Window"));
00049 }
00050
00051 MainWindow::~MainWindow()
00052 {
00053 kDebug() << "destroying mainwindow";
00054 delete d;
00055 }
00056
00057 void MainWindow::setArea(Area *area)
00058 {
00059 bool differentArea = (area != d->area);
00060
00061
00062
00063 d->ignoreDockShown = true;
00064
00065 if (d->autoAreaSettingsSave && differentArea)
00066 saveSettings();
00067
00068 if (d->area)
00069 clearArea();
00070 d->area = area;
00071 d->reconstruct();
00072 d->activateFirstVisibleView();
00073 emit areaChanged(area);
00074 d->ignoreDockShown = false;
00075
00076 loadSettings();
00077 }
00078
00079 void MainWindow::resizeEvent(QResizeEvent* event)
00080 {
00081 return KParts::MainWindow::resizeEvent(event);
00082 }
00083
00084 void MainWindow::clearArea()
00085 {
00086 emit areaCleared(d->area);
00087 d->clearArea();
00088 }
00089
00090 QList<View*> MainWindow::toolDocks() const
00091 {
00092 return d->docks;
00093 }
00094
00095 Area *Sublime::MainWindow::area() const
00096 {
00097 return d->area;
00098 }
00099
00100 Controller *MainWindow::controller() const
00101 {
00102 return d->controller;
00103 }
00104
00105 View *MainWindow::activeView()
00106 {
00107 return d->activeView;
00108 }
00109
00110 View *MainWindow::activeToolView()
00111 {
00112 return d->activeToolView;
00113 }
00114
00115 void MainWindow::activateView(View *view)
00116 {
00117 if (!d->viewContainers.contains(view))
00118 return;
00119 d->viewContainers[view]->setCurrentWidget(view->widget());
00120
00121 setActiveView(view);
00122 }
00123
00124 void MainWindow::setActiveView(View *view)
00125 {
00126 d->activeView = view;
00127 if (view && !view->widget()->hasFocus())
00128 view->widget()->setFocus();
00129 emit activeViewChanged(view);
00130 }
00131
00132 void Sublime::MainWindow::setActiveToolView(View *view)
00133 {
00134 d->activeToolView = view;
00135 emit activeToolViewChanged(view);
00136 }
00137
00138 void MainWindow::saveSettings()
00139 {
00140 QString group = "MainWindow";
00141 if (area())
00142 group += '_' + area()->objectName();
00143 KConfigGroup cg = KGlobal::config()->group(group);
00144
00145
00146 saveMainWindowSettings(cg);
00147 cg.sync();
00148 }
00149
00150 void MainWindow::loadSettings()
00151 {
00152 kDebug() << "loading settings for " << (area() ? area()->objectName() : "");
00153 QString group = "MainWindow";
00154 if (area())
00155 group += '_' + area()->objectName();
00156 KConfigGroup cg = KGlobal::config()->group(group);
00157
00158
00159
00160
00161 QStatusBar* sb = qFindChild<KStatusBar *>(this);
00162 if (sb) {
00163 QString entry = cg.readEntry("StatusBar", "Enabled");
00164 if ( entry == "Disabled" )
00165 sb->hide();
00166 else
00167 sb->show();
00168 }
00169
00170 QMenuBar* mb = qFindChild<KMenuBar *>(this);
00171 if (mb) {
00172 QString entry = cg.readEntry ("MenuBar", "Enabled");
00173 if ( entry == "Disabled" )
00174 mb->hide();
00175 else
00176 mb->show();
00177 }
00178
00179 if ( !autoSaveSettings() || cg.name() == autoSaveGroup() ) {
00180 QString entry = cg.readEntry ("ToolBarsMovable", "Enabled");
00181 if ( entry == "Disabled" )
00182 KToolBar::setToolBarsLocked(true);
00183 else
00184 KToolBar::setToolBarsLocked(false);
00185 }
00186
00187 int n = 1;
00188 foreach (KToolBar* toolbar, toolBars()) {
00189 QString group("Toolbar");
00190
00191
00192 group += (toolbar->objectName().isEmpty() ? QString::number(n) : QString(" ")+toolbar->objectName());
00193
00194 KConfigGroup toolbarGroup(&cg, group);
00195 toolbar->applySettings(toolbarGroup, false);
00196 n++;
00197 }
00198
00199
00200
00201
00202
00203
00204 if (cg.hasKey("State")) {
00205 QByteArray state;
00206 state = cg.readEntry("State", state);
00207 state = QByteArray::fromBase64(state);
00208
00209 restoreState(state);
00210 }
00211
00212 KConfigGroup uiGroup = KGlobal::config()->group("UiSettings");
00213 foreach (Container *container, findChildren<Container*>())
00214 container->setTabBarHidden(uiGroup.readEntry("TabBarVisibility", 1) == 0);
00215
00216 cg.sync();
00217
00218 emit settingsLoaded();
00219 }
00220
00221 bool MainWindow::queryClose()
00222 {
00223
00224 KConfigGroup config(KGlobal::config(), "Main Window");
00225 saveGeometry(config);
00226 config.sync();
00227
00228 return KParts::MainWindow::queryClose();
00229 }
00230
00231 void Sublime::MainWindow::setStatusIcon(View * view, const QIcon & icon)
00232 {
00233 d->setStatusIcon(view, icon);
00234 }
00235
00236 void MainWindow::saveGeometry(KConfigGroup &config)
00237 {
00238 int scnum = QApplication::desktop()->screenNumber(parentWidget());
00239 QRect desk = QApplication::desktop()->screenGeometry(scnum);
00240
00241
00242 if (QApplication::desktop()->isVirtualDesktop())
00243 desk = QApplication::desktop()->screenGeometry(QApplication::desktop()->screen());
00244
00245 QString key = QString::fromLatin1("Desktop %1 %2")
00246 .arg(desk.width()).arg(desk.height());
00247 config.writeEntry(key, geometry());
00248
00249 }
00250 void MainWindow::loadGeometry(const KConfigGroup &config)
00251 {
00252
00253
00254
00255
00256
00257 const int scnum = QApplication::desktop()->screenNumber(parentWidget());
00258 QRect desk = QApplication::desktop()->screenGeometry(scnum);
00259
00260
00261 if (QApplication::desktop()->isVirtualDesktop())
00262 desk = QApplication::desktop()->screenGeometry(QApplication::desktop()->screen());
00263
00264 QString key = QString::fromLatin1("Desktop %1 %2")
00265 .arg(desk.width()).arg(desk.height());
00266 QRect g = config.readEntry(key, QRect());
00267 if (!g.isEmpty())
00268 setGeometry(g);
00269 }
00270
00271 void MainWindow::enableAreaSettingsSave()
00272 {
00273 d->autoAreaSettingsSave = true;
00274 }
00275
00276 }
00277
00278 #include "mainwindow.moc"