File indexing completed on 2024-07-14 05:33:08

0001 /* This file is part of the KDE libraries
0002    Copyright (C) 2005 Christoph Cullmann <cullmann@kde.org>
0003    Copyright (C) 2002, 2003 Joseph Wenninger <jowenn@kde.org>
0004 
0005    GUIClient partly based on ktoolbarhandler.cpp: Copyright (C) 2002 Simon Hausmann <hausmann@kde.org>
0006 
0007    This library is free software; you can redistribute it and/or
0008    modify it under the terms of the GNU Library General Public
0009    License as published by the Free Software Foundation; either
0010    version 2 of the License, or (at your option) any later version.
0011 
0012    This library is distributed in the hope that it will be useful,
0013    but WITHOUT ANY WARRANTY; without even the implied warranty of
0014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015    Library General Public License for more details.
0016 
0017    You should have received a copy of the GNU Library General Public License
0018    along with this library; see the file COPYING.LIB.  If not, write to
0019    the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
0020    Boston, MA 02110-1301, USA.
0021 */
0022 
0023 #include "katemdi.h"
0024 
0025 #include <KLocalizedString>
0026 #include <KSharedConfig>
0027 //#include <kpopupmenu.h>
0028 #include <KActionCollection>
0029 #include <KConfigGroup>
0030 #include <KWindowConfig>
0031 #include <KXMLGUIFactory>
0032 
0033 // #include <q3intdict.h>
0034 #include <QContextMenuEvent>
0035 #include <QEvent>
0036 #include <QHBoxLayout>
0037 #include <QIcon>
0038 #include <QMenu>
0039 #include <QVBoxLayout>
0040 
0041 #include <ktechlab_debug.h>
0042 
0043 typedef QList<int> IntList;
0044 
0045 static QString getSpaces(int count)
0046 {
0047     QString s;
0048     for (int i = 0; i < count; ++i) {
0049         s += "| ";
0050     }
0051     return s;
0052 }
0053 
0054 inline void printObjChildren(int startLevel, const QObject *obj)
0055 {
0056     QObjectList chl = obj->children();
0057     QMutableListIterator<QObject *> itCh(chl);
0058     while (itCh.hasNext()) {
0059         itCh.next();
0060         qCDebug(KTL_LOG) << getSpaces(startLevel) << itCh.value();
0061         printObjChildren(startLevel + 1, itCh.value());
0062     }
0063 }
0064 
0065 namespace KateMDI
0066 {
0067 // BEGIN SPLITTER
0068 
0069 Splitter::Splitter(Qt::Orientation o, QWidget *parent)
0070     : QSplitter(o, parent)
0071 {
0072 }
0073 
0074 Splitter::~Splitter()
0075 {
0076 }
0077 /*
0078 bool Splitter::isLastChild(QWidget* w) const
0079 {
0080   return indexOf(w) == (count() - 1);
0081   //return ( idAfter( w ) == 0 );
0082 }
0083 
0084 int Splitter::idAfter ( QWidget * w ) const
0085 {
0086   //return QSplitter::idAfter (w);
0087     return indexOf(w);
0088 }*/
0089 
0090 // END SPLITTER
0091 
0092 // BEGIN GUICLIENT
0093 
0094 GUIClient::GUIClient(MainWindow *mw)
0095     : QObject(mw)
0096     , KXMLGUIClient(mw)
0097     , m_mw(mw)
0098 {
0099     connect(m_mw->guiFactory(), &KXMLGUIFactory::clientAdded, this, &GUIClient::clientAdded);
0100 
0101     // if (actionCollection()->kaccel()==0)  // TODO what does this do?
0102     {
0103         actionCollection()->associateWidget(m_mw);
0104     }
0105 
0106     // read shortcuts
0107     // actionCollection()->readShortcutSettings( "Shortcuts", kapp->config() );
0108     KConfigGroup grShortcuts = KSharedConfig::openConfig()->group("Shortcuts");
0109     actionCollection()->readSettings(&grShortcuts);
0110 }
0111 
0112 GUIClient::~GUIClient()
0113 {
0114 }
0115 
0116 void GUIClient::registerToolView(ToolView *tv)
0117 {
0118     QString aname = QString("kate_mdi_toolview_") + tv->id;
0119 
0120     // try to read the action shortcut
0121     //   KShortcut sc;
0122 
0123     //   KConfig *cfg = kapp->config();
0124     //   QString _grp = cfg->group();
0125     //   cfg->setGroup("Shortcuts");
0126     //   sc = KShortcut( cfg->readEntry( aname, "" ) );
0127     //   cfg->setGroup( _grp );
0128 
0129     KConfigGroup grSh = KSharedConfig::openConfig()->group("Shortcuts");
0130     //   sc = KShortcut( grSh.readEntry(aname, "") );
0131 }
0132 
0133 void GUIClient::clientAdded(KXMLGUIClient *client)
0134 {
0135     if (client == this)
0136         updateActions();
0137 }
0138 
0139 void GUIClient::updateActions()
0140 {
0141     if (!factory())
0142         return;
0143 }
0144 
0145 // END GUICLIENT
0146 
0147 // BEGIN TOOLVIEW
0148 
0149 ToolView::ToolView(MainWindow *mainwin, Sidebar *sidebar, QWidget *parent)
0150     : QWidget(parent)
0151     , m_mainWin(mainwin)
0152     , m_sidebar(sidebar)
0153     , m_visible(false)
0154     , persistent(false)
0155 {
0156     QVBoxLayout *vboxLayout = new QVBoxLayout;
0157     setLayout(vboxLayout);
0158 }
0159 
0160 ToolView::~ToolView()
0161 {
0162     m_mainWin->toolViewDeleted(this);
0163 }
0164 
0165 void ToolView::setVisibleToolView(bool vis)
0166 {
0167     if (m_visible == vis)
0168         return;
0169 
0170     m_visible = vis;
0171     emit visibleChanged(m_visible);
0172 }
0173 
0174 bool ToolView::visible() const
0175 {
0176     return m_visible;
0177 }
0178 
0179 void ToolView::childEvent(QChildEvent *ev)
0180 {
0181     // set the widget to be focus proxy if possible
0182     QWidget *childWidget = qobject_cast<QWidget *>(ev->child());
0183     if ((ev->type() == QEvent::ChildAdded) && ev->child() && childWidget) {
0184         // setFocusProxy ((QWidget *)(ev->child()->qt_cast("QWidget")));
0185         setFocusProxy(childWidget);
0186     }
0187 
0188     QWidget::childEvent(ev);
0189 }
0190 
0191 // END TOOLVIEW
0192 
0193 // BEGIN SIDEBAR
0194 
0195 Sidebar::Sidebar(KMultiTabBar::KMultiTabBarPosition pos, MainWindow *mainwin, QWidget *parent)
0196     : KMultiTabBar(
0197           //(pos == KMultiTabBar::Top || pos == KMultiTabBar::Bottom) ? KMultiTabBar::Horizontal : KMultiTabBar::Vertical, parent)
0198           pos,
0199           parent)
0200     , m_mainWin(mainwin)
0201     , m_splitter(nullptr)
0202     , m_ownSplit(nullptr)
0203     , m_lastSize(0)
0204 {
0205     setObjectName((QString("Sidebar-%1").arg(pos)).toLatin1().data());
0206     setSidebarPosition(pos);
0207     setFocusPolicy(Qt::NoFocus);
0208     hide();
0209 }
0210 
0211 Sidebar::~Sidebar()
0212 {
0213 }
0214 
0215 void Sidebar::setSidebarPosition(KMultiTabBarPosition pos)
0216 {
0217     m_pos = pos;
0218     setPosition(pos);
0219 }
0220 
0221 void Sidebar::setSidebarStyle(KMultiTabBarStyle style)
0222 {
0223     m_sidebarTabStyle = style;
0224     setStyle(style);
0225 }
0226 
0227 void Sidebar::setSplitter(Splitter *sp)
0228 {
0229     m_splitter = sp;
0230     Qt::Orientation splitOrient = (sidebarPosition() == KMultiTabBar::Top || sidebarPosition() == KMultiTabBar::Bottom) ? Qt::Horizontal : Qt::Vertical;
0231     m_ownSplit = new Splitter(splitOrient, m_splitter);
0232     m_ownSplit->setObjectName("own-Split");
0233     m_ownSplit->setChildrenCollapsible(false);
0234     // m_splitter->setResizeMode( m_ownSplit, QSplitter::KeepSize ); // 2018.11.22
0235     m_splitter->setStretchFactor(m_splitter->indexOf(m_ownSplit), 0);
0236     m_ownSplit->hide();
0237 }
0238 
0239 ToolView *Sidebar::addWidget(const QIcon &icon, const QString &text, ToolView *widget)
0240 {
0241     static int id = 0;
0242 
0243     if (widget) {
0244         if (widget->sidebar() == this)
0245             return widget;
0246 
0247         widget->sidebar()->removeWidget(widget);
0248     }
0249 
0250     int newId = ++id;
0251 
0252     appendTab(icon, newId, text);
0253 
0254     if (!widget) {
0255         widget = new ToolView(m_mainWin, this, m_ownSplit);
0256         widget->hide();
0257         widget->icon = icon;
0258         widget->text = text;
0259     } else {
0260         widget->hide();
0261         // widget->reparent (m_ownSplit, 0, QPoint()); // 2018.11.22
0262         widget->setParent(m_ownSplit);
0263         QPoint p;
0264         widget->setGeometry(p.x(), p.y(), width(), height());
0265         widget->m_sidebar = this;
0266     }
0267 
0268     // save it's pos ;)
0269     widget->persistent = false;
0270 
0271     m_idToWidget.insert(newId, widget);
0272     m_widgetToId.insert(widget, newId);
0273     m_toolviews.push_back(widget);
0274 
0275     show();
0276 
0277     connect(tab(newId), qOverload<int>(&KMultiTabBarTab::clicked), this, &Sidebar::tabClicked);
0278     tab(newId)->installEventFilter(this);
0279 
0280     return widget;
0281 }
0282 
0283 void Sidebar::updateMinimumSize()
0284 {
0285     //  qCDebug(KTL_LOG) << "layout()->margin()="<<layout()->margin();
0286 
0287     QSize minSize;
0288 
0289     QList<ToolView *>::iterator end = m_toolviews.end();
0290     for (QList<ToolView *>::iterator it = m_toolviews.begin(); it != end; ++it) {
0291         QSize s = (*it)->childrenRect().size();
0292         minSize = minSize.expandedTo(s);
0293         //      qCDebug(KTL_LOG) << "s="<<s<<"(*it)->minimumSize()="<<(*it)->minimumSize();
0294         //      qCDebug(KTL_LOG) << "(*it)->layout()->margin()="<<(*it)->margin();
0295     }
0296 
0297     minSize.setWidth(minSize.width() - 30);
0298     minSize.setHeight(minSize.height() - 30);
0299 
0300     for (QList<ToolView *>::iterator it = m_toolviews.begin(); it != end; ++it) {
0301         (*it)->setMinimumSize(minSize);
0302     }
0303 }
0304 
0305 bool Sidebar::removeWidget(ToolView *widget)
0306 {
0307     if (!m_widgetToId.contains(widget))
0308         return false;
0309 
0310     removeTab(m_widgetToId[widget]);
0311 
0312     m_idToWidget.remove(m_widgetToId[widget]);
0313     m_widgetToId.remove(widget);
0314     m_toolviews.removeAll(widget);
0315 
0316     bool anyVis = false;
0317     // Q3IntDictIterator<ToolView> it( m_idToWidget );
0318 
0319     for (QMap<int, ToolView *>::iterator it = m_idToWidget.begin(); it != m_idToWidget.end(); ++it) {
0320         if (!anyVis)
0321             anyVis = it.value()->isVisible();
0322     }
0323 
0324     if (m_idToWidget.isEmpty()) {
0325         m_ownSplit->hide();
0326         hide();
0327     } else if (!anyVis)
0328         m_ownSplit->hide();
0329 
0330     return true;
0331 }
0332 
0333 bool Sidebar::showWidget(ToolView *widget)
0334 {
0335     if (!m_widgetToId.contains(widget))
0336         return false;
0337 
0338     // hide other non-persistent views
0339     // Q3IntDictIterator<ToolView> it( m_idToWidget );
0340     for (QMap<int, ToolView *>::iterator it = m_idToWidget.begin(); it != m_idToWidget.end(); ++it)
0341         if ((it.value() != widget) && !it.value()->persistent) {
0342             it.value()->hide();
0343             setTab(it.key(), false);
0344             it.value()->setVisibleToolView(false);
0345         }
0346 
0347     setTab(m_widgetToId[widget], true);
0348 
0349     m_ownSplit->show();
0350     widget->show();
0351 
0352     widget->setVisibleToolView(true);
0353 
0354     return true;
0355 }
0356 
0357 bool Sidebar::hideWidget(ToolView *widget)
0358 {
0359     if (!m_widgetToId.contains(widget))
0360         return false;
0361 
0362     bool anyVis = false;
0363 
0364     updateLastSize();
0365 
0366     // for ( Q3IntDictIterator<ToolView> it( m_idToWidget ); it.current(); ++it )
0367     for (QMap<int, ToolView *>::iterator it = m_idToWidget.begin(); it != m_idToWidget.end(); ++it) {
0368         if (it.value() == widget) {
0369             it.value()->hide();
0370             continue;
0371         }
0372 
0373         if (!anyVis)
0374             anyVis = it.value()->isVisible();
0375     }
0376 
0377     // lower tab
0378     setTab(m_widgetToId[widget], false);
0379 
0380     if (!anyVis)
0381         m_ownSplit->hide();
0382 
0383     widget->setVisibleToolView(false);
0384 
0385     return true;
0386 }
0387 
0388 void Sidebar::tabClicked(int i)
0389 {
0390     ToolView *w = m_idToWidget[i];
0391 
0392     if (!w) {
0393         qCWarning(KTL_LOG) << " unexpected tab number " << i;
0394         return;
0395     }
0396 
0397     if (isTabRaised(i)) {
0398         showWidget(w);
0399         w->setFocus();
0400     } else {
0401         hideWidget(w);
0402         m_mainWin->centralWidget()->setFocus();
0403     }
0404 }
0405 
0406 bool Sidebar::eventFilter(QObject *obj, QEvent *ev)
0407 {
0408     if (ev->type() == QEvent::ContextMenu) {
0409         QContextMenuEvent *e = static_cast<QContextMenuEvent *>(ev);
0410         KMultiTabBarTab *bt = dynamic_cast<KMultiTabBarTab *>(obj);
0411         if (bt) {
0412             qCDebug(KTL_LOG) << "Request for popup";
0413 
0414             m_popupButton = bt->id();
0415 
0416             ToolView *w = m_idToWidget[m_popupButton];
0417 
0418             if (w) {
0419                 QMenu *p = new QMenu(this);
0420 
0421                 p->addSection(QIcon::fromTheme("view_remove"), i18n("Behavior"));
0422 
0423                 // p->insertItem(w->persistent ? SmallIcon("view-restore") : SmallIcon("view-fullscreen"), w->persistent ? i18n("Make Non-Persistent") : i18n("Make Persistent"), 10); // 2018.11.22
0424                 p->addAction(w->persistent ? QIcon::fromTheme("view-restore") : QIcon::fromTheme("view-fullscreen"), w->persistent ? i18n("Make Non-Persistent") : i18n("Make Persistent"))->setData(10);
0425 
0426                 p->addSection(QIcon::fromTheme("transform-move"), i18n("Move To"));
0427 
0428                 if (sidebarPosition() != 0)
0429                     // p->insertItem(SmallIcon("go-previous"), i18n("Left Sidebar"),0);  // 2018.11.22
0430                     p->addAction(QIcon::fromTheme("go-previous"), i18n("Left Sidebar"))->setData(0);
0431 
0432                 if (sidebarPosition() != 1)
0433                     // p->insertItem(SmallIcon("go-next"), i18n("Right Sidebar"),1); // 2018.11.22
0434                     p->addAction(QIcon::fromTheme("go-next"), i18n("Right Sidebar"))->setData(1);
0435 
0436                 if (sidebarPosition() != 2)
0437                     // p->insertItem(SmallIcon("go-up"), i18n("Top Sidebar"),2); // 2018.11.22
0438                     p->addAction(QIcon::fromTheme("go-up"), i18n("Top Sidebar"))->setData(2);
0439 
0440                 if (sidebarPosition() != 3) {
0441                     // p->insertItem(SmallIcon("go-down"), i18n("Bottom Sidebar"),3); // 2018.11.22
0442                     p->addAction(QIcon::fromTheme("go-down"), i18n("Bottom Sidebar"))->setData(3);
0443                 }
0444                 connect(p, &QMenu::triggered, this, &Sidebar::buttonPopupActivate);
0445 
0446                 p->exec(e->globalPos());
0447                 delete p;
0448 
0449                 return true;
0450             }
0451         }
0452     }
0453 
0454     return false;
0455 }
0456 
0457 void Sidebar::buttonPopupActivate(QAction *action)
0458 {
0459     int id = action->data().toInt();
0460     ToolView *w = m_idToWidget[m_popupButton];
0461 
0462     if (!w)
0463         return;
0464 
0465     // move ids
0466     if (id < 4) {
0467         // move + show ;)
0468         m_mainWin->moveToolView(w, KMultiTabBar::KMultiTabBarPosition(id));
0469         m_mainWin->showToolView(w);
0470     }
0471 
0472     // toggle persistent
0473     if (id == 10)
0474         w->persistent = !w->persistent;
0475 }
0476 
0477 void Sidebar::updateLastSize()
0478 {
0479     QList<int> s = m_splitter->sizes();
0480 
0481     int i = 0;
0482     if ((sidebarPosition() == KMultiTabBar::Right || sidebarPosition() == KMultiTabBar::Bottom))
0483         i = 2;
0484 
0485     // little threshold
0486     if (s[i] > 2)
0487         m_lastSize = s[i];
0488 }
0489 
0490 class TmpToolViewSorter
0491 {
0492 public:
0493     ToolView *tv;
0494     unsigned int pos;
0495 };
0496 
0497 void Sidebar::restoreSession(KConfigGroup *configGr)
0498 {
0499     // get the last correct placed toolview
0500     int firstWrong = 0;
0501     for (; firstWrong < m_toolviews.size(); ++firstWrong) {
0502         ToolView *tv = m_toolviews[firstWrong];
0503 
0504         int pos = configGr->readEntry(QString("Kate-MDI-ToolView-%1-Sidebar-Position").arg(tv->id), firstWrong);
0505 
0506         if (pos != firstWrong)
0507             break;
0508     }
0509 
0510     // we need to reshuffle, ahhh :(
0511     if (firstWrong < m_toolviews.size()) {
0512         // first: collect the items to reshuffle
0513         QList<TmpToolViewSorter> toSort;
0514         for (int i = firstWrong; i < m_toolviews.size(); ++i) {
0515             TmpToolViewSorter s;
0516             s.tv = m_toolviews[i];
0517             s.pos = configGr->readEntry(QString("Kate-MDI-ToolView-%1-Sidebar-Position").arg(m_toolviews[i]->id), i);
0518             toSort.push_back(s);
0519         }
0520 
0521         // now: sort the stuff we need to reshuffle
0522         for (int m = 0; m < toSort.size(); ++m)
0523             for (int n = m + 1; n < toSort.size(); ++n)
0524                 if (toSort[n].pos < toSort[m].pos) {
0525                     TmpToolViewSorter tmp = toSort[n];
0526                     toSort[n] = toSort[m];
0527                     toSort[m] = tmp;
0528                 }
0529 
0530         // then: remove this items from the button bar
0531         // do this backwards, to minimize the relayout efforts
0532         for (int i = m_toolviews.size() - 1; i >= int(firstWrong); --i) {
0533             removeTab(m_widgetToId[m_toolviews[i]]);
0534         }
0535 
0536         // insert the reshuffled things in order :)
0537         for (int i = 0; i < toSort.size(); ++i) {
0538             ToolView *tv = toSort[i].tv;
0539 
0540             m_toolviews[firstWrong + i] = tv;
0541 
0542             // readd the button
0543             int newId = m_widgetToId[tv];
0544             appendTab(tv->icon, newId, tv->text);
0545             connect(tab(newId), qOverload<int>(&KMultiTabBarTab::clicked), this, &Sidebar::tabClicked);
0546             tab(newId)->installEventFilter(this);
0547 
0548             // reshuffle in splitter
0549             m_ownSplit->addWidget(tv);
0550         }
0551     }
0552 
0553     // update last size if needed
0554     updateLastSize();
0555 
0556     // restore the own splitter sizes
0557     QList<int> s = configGr->readEntry(QString("Kate-MDI-Sidebar-%1-Splitter").arg(sidebarPosition()), IntList());
0558     m_ownSplit->setSizes(s);
0559 
0560     // show only correct toolviews, remember persistent values ;)
0561     bool anyVis = false;
0562     for (int i = 0; i < m_toolviews.size(); ++i) {
0563         ToolView *tv = m_toolviews[i];
0564 
0565         tv->persistent = configGr->readEntry(QString("Kate-MDI-ToolView-%1-Persistent").arg(tv->id), false);
0566         tv->setVisibleToolView(configGr->readEntry(QString("Kate-MDI-ToolView-%1-Visible").arg(tv->id), false));
0567 
0568         if (!anyVis)
0569             anyVis = tv->visible();
0570 
0571         setTab(m_widgetToId[tv], tv->visible());
0572 
0573         if (tv->visible())
0574             tv->show();
0575         else
0576             tv->hide();
0577     }
0578 
0579     if (anyVis)
0580         m_ownSplit->show();
0581     else
0582         m_ownSplit->hide();
0583 }
0584 
0585 void Sidebar::saveSession(KConfigGroup *config)
0586 {
0587     // store the own splitter sizes
0588     QList<int> s = m_ownSplit->sizes();
0589     config->writeEntry(QString("Kate-MDI-Sidebar-%1-Splitter").arg(sidebarPosition()), s);
0590 
0591     // store the data about all toolviews in this sidebar ;)
0592     for (int i = 0; i < m_toolviews.size(); ++i) {
0593         ToolView *tv = m_toolviews[i];
0594 
0595         config->writeEntry(QString("Kate-MDI-ToolView-%1-Position").arg(tv->id), int(tv->sidebar()->sidebarPosition()));
0596         config->writeEntry(QString("Kate-MDI-ToolView-%1-Sidebar-Position").arg(tv->id), i);
0597         config->writeEntry(QString("Kate-MDI-ToolView-%1-Visible").arg(tv->id), tv->visible());
0598         config->writeEntry(QString("Kate-MDI-ToolView-%1-Persistent").arg(tv->id), tv->persistent);
0599     }
0600 }
0601 
0602 // END SIDEBAR
0603 
0604 // BEGIN MAIN WINDOW
0605 
0606 MainWindow::MainWindow(QWidget *parentWidget)
0607     : KParts::MainWindow(parentWidget)
0608     , m_restoreConfig(nullptr)
0609     , m_guiClient(new GUIClient(this))
0610 {
0611     // init the internal widgets
0612     QWidget *hb = new QWidget(this); // Q3HBox (this);
0613     QHBoxLayout *hbl = new QHBoxLayout;
0614     hb->setLayout(hbl);
0615 
0616     hb->setObjectName("MainWindow-central-HBox");
0617     setCentralWidget(hb);
0618 
0619     m_sidebars[KMultiTabBar::Left] = new Sidebar(KMultiTabBar::Left, this, hb);
0620     m_sidebars[KMultiTabBar::Left]->setObjectName("Main-Left-Sidebar");
0621     hbl->addWidget(m_sidebars[KMultiTabBar::Left]);
0622 
0623     m_hSplitter = new Splitter(Qt::Horizontal, hb);
0624     m_hSplitter->setObjectName("Main-Left-Splitter");
0625     hbl->addWidget(m_hSplitter);
0626 
0627     m_sidebars[KMultiTabBar::Left]->setSplitter(m_hSplitter);
0628 
0629     // Q3VBox *vb = new Q3VBox (m_hSplitter);
0630     QWidget *vb = new QWidget(m_hSplitter);
0631     QVBoxLayout *vbl = new QVBoxLayout;
0632     vb->setLayout(vbl);
0633     vb->setObjectName("Main-Center-VBox");
0634     m_hSplitter->setCollapsible(m_hSplitter->indexOf(vb), false);
0635 
0636     m_sidebars[KMultiTabBar::Top] = new Sidebar(KMultiTabBar::Top, this, vb);
0637     m_sidebars[KMultiTabBar::Top]->setObjectName("Main-Top-Sidebar");
0638     vbl->addWidget(m_sidebars[KMultiTabBar::Top]);
0639 
0640     m_vSplitter = new Splitter(Qt::Vertical, vb);
0641     m_vSplitter->setObjectName("Main-Top-Splitter");
0642     vbl->addWidget(m_vSplitter);
0643 
0644     m_sidebars[KMultiTabBar::Top]->setSplitter(m_vSplitter);
0645 
0646     m_centralWidget = new QWidget(m_vSplitter);
0647     // m_centralWidget = new Q3VBox (m_vSplitter );
0648     QVBoxLayout *vbCl = new QVBoxLayout;
0649     m_centralWidget->setLayout(vbCl);
0650     m_centralWidget->setObjectName("Main-Central-Vbox");
0651     m_vSplitter->setCollapsible(m_vSplitter->indexOf(m_centralWidget), false);
0652     // vbl->addLayout( vbCl ); // 2016.05.03 - apparently generates a warning about already having a parent
0653 
0654     m_sidebars[KMultiTabBar::Bottom] = new Sidebar(KMultiTabBar::Bottom, this, vb);
0655     m_sidebars[KMultiTabBar::Bottom]->setObjectName("Main-Bottom-Sidebar");
0656     m_sidebars[KMultiTabBar::Bottom]->setSplitter(m_vSplitter);
0657     vbl->addWidget(m_sidebars[KMultiTabBar::Bottom]);
0658 
0659     m_sidebars[KMultiTabBar::Right] = new Sidebar(KMultiTabBar::Right, this, hb);
0660     m_sidebars[KMultiTabBar::Right]->setObjectName("Main-Right-Sidebar");
0661     m_sidebars[KMultiTabBar::Right]->setSplitter(m_hSplitter);
0662     hbl->addWidget(m_sidebars[KMultiTabBar::Right]);
0663 }
0664 
0665 MainWindow::~MainWindow()
0666 {
0667     // cu toolviews
0668     while (!m_toolviews.isEmpty())
0669         delete m_toolviews[0];
0670 
0671     // seems like we really should delete this by hand ;)
0672     delete m_centralWidget;
0673 
0674     for (unsigned int i = 0; i < 4; ++i)
0675         delete m_sidebars[i];
0676 }
0677 
0678 QWidget *MainWindow::centralWidget() const
0679 {
0680     return m_centralWidget;
0681 }
0682 
0683 ToolView *MainWindow::createToolView(const QString &identifier, KMultiTabBar::KMultiTabBarPosition pos, const QIcon &icon, const QString &text)
0684 {
0685     if (m_idToWidget[identifier])
0686         return nullptr;
0687 
0688     // try the restore config to figure out real pos
0689     if (m_restoreConfig && m_restoreConfig->hasGroup(m_restoreGroup)) {
0690         KConfigGroup grRest = m_restoreConfig->group(m_restoreGroup);
0691         pos = KMultiTabBar::KMultiTabBarPosition(grRest.readEntry(QString("Kate-MDI-ToolView-%1-Position").arg(identifier), int(pos)));
0692     }
0693 
0694     ToolView *v = m_sidebars[pos]->addWidget(icon, text, nullptr);
0695     v->id = identifier;
0696 
0697     m_idToWidget.insert(identifier, v);
0698     m_toolviews.push_back(v);
0699 
0700     // register for menu stuff
0701     m_guiClient->registerToolView(v);
0702 
0703     return v;
0704 }
0705 
0706 ToolView *MainWindow::toolView(const QString &identifier) const
0707 {
0708     return m_idToWidget[identifier];
0709 }
0710 
0711 void MainWindow::toolViewDeleted(ToolView *widget)
0712 {
0713     if (!widget)
0714         return;
0715 
0716     if (widget->mainWindow() != this)
0717         return;
0718 
0719     // unregister from menu stuff
0720 
0721     widget->sidebar()->removeWidget(widget);
0722 
0723     m_idToWidget.remove(widget->id);
0724     m_toolviews.removeAll(widget);
0725 }
0726 
0727 void MainWindow::setToolViewStyle(KMultiTabBar::KMultiTabBarStyle style)
0728 {
0729     m_sidebars[0]->setSidebarStyle(style);
0730     m_sidebars[1]->setSidebarStyle(style);
0731     m_sidebars[2]->setSidebarStyle(style);
0732     m_sidebars[3]->setSidebarStyle(style);
0733 }
0734 
0735 KMultiTabBar::KMultiTabBarStyle MainWindow::toolViewStyle() const
0736 {
0737     // all sidebars have the same style, so just take Top
0738     return m_sidebars[KMultiTabBar::Top]->sidebarTabStyle();
0739 }
0740 
0741 bool MainWindow::moveToolView(ToolView *widget, KMultiTabBar::KMultiTabBarPosition pos)
0742 {
0743     if (!widget || widget->mainWindow() != this)
0744         return false;
0745 
0746     // try the restore config to figure out real pos
0747     if (m_restoreConfig && m_restoreConfig->hasGroup(m_restoreGroup)) {
0748         KConfigGroup grRest = m_restoreConfig->group(m_restoreGroup);
0749         pos = KMultiTabBar::KMultiTabBarPosition(grRest.readEntry(QString("Kate-MDI-ToolView-%1-Position").arg(widget->id), int(pos)));
0750     }
0751 
0752     m_sidebars[pos]->addWidget(widget->icon, widget->text, widget);
0753 
0754     return true;
0755 }
0756 
0757 bool MainWindow::showToolView(ToolView *widget)
0758 {
0759     if (!widget || widget->mainWindow() != this)
0760         return false;
0761 
0762     // skip this if happens during restoring, or we will just see flicker
0763     if (m_restoreConfig && m_restoreConfig->hasGroup(m_restoreGroup))
0764         return true;
0765 
0766     return widget->sidebar()->showWidget(widget);
0767 }
0768 
0769 bool MainWindow::hideToolView(ToolView *widget)
0770 {
0771     if (!widget || widget->mainWindow() != this)
0772         return false;
0773 
0774     // skip this if happens during restoring, or we will just see flicker
0775     if (m_restoreConfig && m_restoreConfig->hasGroup(m_restoreGroup))
0776         return true;
0777 
0778     return widget->sidebar()->hideWidget(widget);
0779 }
0780 
0781 void MainWindow::startRestore(KConfig *config, const QString &group)
0782 {
0783     // first save this stuff
0784     m_restoreConfig = config;
0785     m_restoreGroup = group;
0786 
0787     if (!m_restoreConfig || !m_restoreConfig->hasGroup(m_restoreGroup)) {
0788         // BEGIN Added stuff specifically for ktechlab
0789         QList<int> hs;
0790         hs << 220 << 100 << 230;
0791         QList<int> vs;
0792         vs << 0 << 100 << 150;
0793 
0794         m_sidebars[0]->setLastSize(hs[0]);
0795         m_sidebars[1]->setLastSize(hs[2]);
0796         m_sidebars[2]->setLastSize(vs[0]);
0797         m_sidebars[3]->setLastSize(vs[2]);
0798 
0799         m_hSplitter->setSizes(hs);
0800         m_vSplitter->setSizes(vs);
0801         // END Added stuff specifically for ktechlab
0802 
0803         return;
0804     }
0805 
0806     // apply size once, to get sizes ready ;)
0807     KConfigGroup grRestWnd = m_restoreConfig->group(m_restoreGroup);
0808     KWindowConfig::restoreWindowSize(windowHandle(), grRestWnd);
0809 
0810     // m_restoreConfig->group (m_restoreGroup);
0811 
0812     // get main splitter sizes ;)
0813     QList<int> hs = grRestWnd.readEntry("Kate-MDI-H-Splitter", IntList());
0814     QList<int> vs = grRestWnd.readEntry("Kate-MDI-V-Splitter", IntList());
0815 
0816     m_sidebars[0]->setLastSize(hs[0]);
0817     m_sidebars[1]->setLastSize(hs[2]);
0818     m_sidebars[2]->setLastSize(vs[0]);
0819     m_sidebars[3]->setLastSize(vs[2]);
0820 
0821     m_hSplitter->setSizes(hs);
0822     m_vSplitter->setSizes(vs);
0823 
0824     setToolViewStyle(KMultiTabBar::KMultiTabBarStyle(grRestWnd.readEntry("Kate-MDI-Sidebar-Style", int(toolViewStyle()))));
0825 }
0826 
0827 void MainWindow::finishRestore()
0828 {
0829     if (!m_restoreConfig)
0830         return;
0831 
0832     if (m_restoreConfig->hasGroup(m_restoreGroup)) {
0833         // apply all settings, like toolbar pos and more ;)
0834         applyMainWindowSettings(m_restoreConfig->group(m_restoreGroup));
0835 
0836         // reshuffle toolviews only if needed
0837         KConfigGroup grRest = m_restoreConfig->group(m_restoreGroup);
0838         for (int i = 0; i < m_toolviews.size(); ++i) {
0839             KMultiTabBar::KMultiTabBarPosition newPos = KMultiTabBar::KMultiTabBarPosition(grRest.readEntry(QString("Kate-MDI-ToolView-%1-Position").arg(m_toolviews[i]->id), int(m_toolviews[i]->sidebar()->sidebarPosition())));
0840 
0841             if (m_toolviews[i]->sidebar()->sidebarPosition() != newPos) {
0842                 moveToolView(m_toolviews[i], newPos);
0843             }
0844         }
0845 
0846         // restore the sidebars
0847         for (unsigned int i = 0; i < 4; ++i)
0848             m_sidebars[i]->restoreSession(&grRest);
0849     }
0850 
0851     // clear this stuff, we are done ;)
0852     m_restoreConfig = nullptr;
0853     m_restoreGroup = "";
0854 }
0855 
0856 void MainWindow::saveSession(KConfigGroup *grConf)
0857 {
0858     if (!grConf)
0859         return;
0860 
0861     saveMainWindowSettings(*grConf);
0862 
0863     // save main splitter sizes ;)
0864     QList<int> hs = m_hSplitter->sizes();
0865     QList<int> vs = m_vSplitter->sizes();
0866 
0867     if (hs[0] <= 2 && !m_sidebars[0]->splitterVisible())
0868         hs[0] = m_sidebars[0]->lastSize();
0869     if (hs[2] <= 2 && !m_sidebars[1]->splitterVisible())
0870         hs[2] = m_sidebars[1]->lastSize();
0871     if (vs[0] <= 2 && !m_sidebars[2]->splitterVisible())
0872         vs[0] = m_sidebars[2]->lastSize();
0873     if (vs[2] <= 2 && !m_sidebars[3]->splitterVisible())
0874         vs[2] = m_sidebars[3]->lastSize();
0875 
0876     grConf->writeEntry("Kate-MDI-H-Splitter", hs);
0877     grConf->writeEntry("Kate-MDI-V-Splitter", vs);
0878 
0879     // save sidebar style
0880     grConf->writeEntry("Kate-MDI-Sidebar-Style", int(toolViewStyle()));
0881 
0882     // save the sidebars
0883     for (unsigned int i = 0; i < 4; ++i)
0884         m_sidebars[i]->saveSession(grConf);
0885 }
0886 
0887 void KateMDI::MainWindow::updateSidebarMinimumSizes()
0888 {
0889     //  for (unsigned int i=0; i < 4; ++i)
0890     //      m_sidebars[i]->updateMinimumSize();
0891     m_sidebars[KMultiTabBar::Right]->updateMinimumSize();
0892 }
0893 
0894 // END MAIN WINDOW
0895 
0896 }
0897 
0898 #include "moc_katemdi.cpp"