[KLF Backend][KLF Tools][KLF Home]
KLatexFormula Project
src/klftools/klfsidewidget.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *   file klfsidewidget.cpp
00003  *   This file is part of the KLatexFormula Project.
00004  *   Copyright (C) 2011 by Philippe Faist
00005  *   philippe.faist at bluewin.ch
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************/
00022 /* $Id$ */
00023 
00024 
00025 #include <QApplication>
00026 #include <QtGlobal>
00027 #include <QLayout>
00028 #include <QEvent>
00029 #include <QResizeEvent>
00030 #include <QStackedLayout>
00031 #include <QPointer>
00032 #include <QTime>
00033 #include <QTimer>
00034 
00035 #include <klfdefs.h>
00036 
00037 #include "klfsidewidget.h"
00038 
00039 #include "klfsidewidget_p.h"
00040 
00041 
00042 
00043 struct KLFSideWidgetManagerBasePrivate
00044 {
00045   KLF_PRIVATE_HEAD(KLFSideWidgetManagerBase)
00046   {
00047     sideWidgetParentConsistency = false;
00048   }
00049 
00050   QPointer<QWidget> pSideWidget;
00051   QPointer<QWidget> pParentWidget;
00052 
00053   bool sideWidgetParentConsistency;
00054 };
00055 
00056 
00057 KLFSideWidgetManagerBase::KLFSideWidgetManagerBase(QWidget *parentWidget, QWidget *sideWidget,
00058                                                    bool consistency, QObject *parent)
00059   : QObject(parent)
00060 {
00061   KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00062 
00063   KLF_INIT_PRIVATE(KLFSideWidgetManagerBase) ;
00064 
00065   d->sideWidgetParentConsistency = consistency;
00066 
00067   // IMPORTANT: see dox doc of this constructor.
00068   d->pSideWidget = NULL;
00069   d->pParentWidget = NULL;
00070 
00071   Q_UNUSED(parentWidget);
00072   Q_UNUSED(sideWidget);
00073 }
00074 
00075 KLFSideWidgetManagerBase::~KLFSideWidgetManagerBase()
00076 {
00077   KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00078 
00079   // with QPointer should be OK...  //  This has to be done manually by caller to avoid crashes at application/widget-destruction time
00080   setSideWidget(NULL);
00081 
00082   KLF_DELETE_PRIVATE ;
00083 }
00084 
00085 QWidget * KLFSideWidgetManagerBase::sideWidget() const
00086 {
00087   return d->pSideWidget;
00088 }
00089 QWidget * KLFSideWidgetManagerBase::ourParentWidget() const
00090 {
00091   return d->pParentWidget;
00092 }
00093 
00094 void KLFSideWidgetManagerBase::setOurParentWidget(QWidget *p)
00095 {
00096   KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00097 
00098   if (d->pParentWidget == p) {
00099     klfDbg("no-op.") ;
00100     return;
00101   }
00102 
00103   klfDbg("old="<<d->pParentWidget<<", new parentWidget="<<p) ;
00104   QWidget *oldpw = d->pParentWidget;
00105   d->pParentWidget = p;
00106   if (d->pSideWidget != NULL && d->sideWidgetParentConsistency) {
00107     d->pSideWidget->setParent(d->pParentWidget);
00108   }
00109   newParentWidgetSet(oldpw, d->pParentWidget);
00110 }
00111 
00112 void KLFSideWidgetManagerBase::setSideWidget(QWidget *sideWidget)
00113 {
00114   KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00115 
00116   if (d->pSideWidget == sideWidget) {
00117     klfDbg("no-op.") ;
00118     return;
00119   }
00120 
00121   klfDbg("old="<<(void*)d->pSideWidget<<", new sideWidget="<<(void*)sideWidget) ;
00122   klfDbg("old="<<d->pSideWidget<<", new sideWidget="<<sideWidget) ;
00123   QWidget *oldw = d->pSideWidget;
00124 
00125   d->pSideWidget = sideWidget;
00126 
00127   if (d->pSideWidget != NULL && d->sideWidgetParentConsistency) {
00128     if (d->pSideWidget->parentWidget() != d->pParentWidget) {
00129       klfDbg("Adjusting side widget's parent to satisfy parent consistency...") ;
00130       d->pSideWidget->setParent(d->pParentWidget);
00131       d->pSideWidget->show(); // subclasses may assume that this object is shown.
00132     }
00133   }
00134 
00135   klfDbg("about to call virtual method.") ;
00136   newSideWidgetSet(oldw, d->pSideWidget);
00137 }
00138 
00139 
00140 void KLFSideWidgetManagerBase::waitForShowHideActionFinished(int timeout_ms)
00141 {
00142   if (!showHideIsAnimating())
00143     return; // we're not animated, we're immediately done.
00144 
00145   QTime tm;
00146 
00147   tm.start();
00148 
00149   //  connect(this, SIGNAL(sideWidgetShown(bool)), this, SLOT(slotSideWidgetShown(bool)));
00150 
00151   // Don't reinitialize to false here, since slotSideWidgetShownActionFinished() could have
00152   // already been called.
00153   //  d->actionFinishedSignalReceived = false; 
00154   while (showHideIsAnimating()) {
00155     qApp->processEvents();
00156     if (tm.elapsed() > timeout_ms) {
00157       klfDbg("timeout while waiting for action-finished signal. timeout_ms="<<timeout_ms) ;
00158       break;
00159     }
00160   }
00161 
00162   klfDbg("finished.");
00163 }
00164 
00165 
00166 
00167 // ------------------
00168 
00169 
00170 
00171 
00172 struct KLFShowHideSideWidgetManagerPrivate
00173 {
00174   KLF_PRIVATE_HEAD(KLFShowHideSideWidgetManager)
00175   {
00176     infunction = false;
00177     oldParent = NULL;
00178     msize = QSize(0, 0);
00179     orientation = Qt::Horizontal;
00180     calcSpacing = 6;
00181   }
00182 
00183   bool infunction;
00184   QWidget *oldParent;
00185   // msie is only used when the side widget is visible
00186   QSize msize;
00187 
00188   Qt::Orientation orientation;
00189   int calcSpacing;
00190 };
00191 
00192 
00193 KLFShowHideSideWidgetManager::KLFShowHideSideWidgetManager(QWidget *parentWidget, QWidget *sideWidget,
00194                                                            QObject *parent)
00195   : KLFSideWidgetManagerBase(parentWidget, sideWidget, true, parent)
00196 {
00197   KLF_INIT_PRIVATE(KLFShowHideSideWidgetManager) ;
00198 
00199   setOurParentWidget(parentWidget);
00200   setSideWidget(sideWidget);
00201 }
00202 
00203 KLFShowHideSideWidgetManager::~KLFShowHideSideWidgetManager()
00204 {
00205   KLF_DELETE_PRIVATE ;
00206 }
00207 
00208 // protected
00209 void KLFShowHideSideWidgetManager::newSideWidgetSet(QWidget *oldw, QWidget *neww)
00210 {
00211   KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00212 
00213   //bool preserveshown = false;
00214   //bool shown = false;
00215 
00216   if (oldw != NULL) {
00217     klfDbg("old="<<oldw) ;
00218     //preserveshown = true;
00219     //shown = oldw->isVisible();
00220     oldw->removeEventFilter(this);
00221     oldw->hide();
00222   }
00223   if (neww != NULL) {
00224     KLF_ASSERT_CONDITION(ourParentWidget() == neww->parentWidget(),
00225                        "Adding a widget that is not a child of our 'parent widget' ! Correcting parent.",
00226                        setOurParentWidget(neww->parentWidget()); ) ;
00227     klfDbg("new="<<neww) ;
00228     //     if (neww->isVisible()) {
00229     //       d->msize = neww->parentWidget()->size();
00230     //       klfDbg("parent size is "<<d->msize);
00231     //       if (d->orientation & Qt::Horizontal)
00232     //  d->msize.setWidth(d->msize.width() - d->calcSpacing + neww->width());
00233     //       if (d->orientation & Qt::Vertical)
00234     //  d->msize.setHeight(d->msize.height() - d->calcSpacing + neww->height());
00235     //       klfDbg("initialized size to "<<d->msize) ;
00236     //     } else {
00237     //       // if widget is about to be shown in initialization process, we will recieve a resizeEvent.
00238     //       // will calculate proper size there.
00239     //       d->msize = QSize();
00240     //     }
00241     if (neww->parentWidget()->layout() != NULL) {
00242       setCalcSpacing(neww->parentWidget()->layout()->spacing());
00243     }
00244     neww->hide(); //unconditionally hide!
00245     //    showSideWidget(false);
00246     //    if (preserveshown && shown)
00247     //      showSideWidget(true);
00248     neww->installEventFilter(this);
00249     emit sideWidgetShown(false);
00250   }
00251   d->msize = QSize();
00252 }
00253 
00254 
00255 // protected
00256 void KLFShowHideSideWidgetManager::newParentWidgetSet(QWidget */*oldParent*/, QWidget *pw)
00257 {
00258   KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00259 
00260   if (d->oldParent != NULL)
00261     d->oldParent->removeEventFilter(this);
00262   if (pw == NULL) {
00263     d->msize = QSize(-1, -1);
00264     return;
00265   }
00266   d->msize = pw->size();
00267   pw->installEventFilter(this);
00268 }
00269 
00270 bool KLFShowHideSideWidgetManager::sideWidgetVisible() const
00271 {
00272   KLF_ASSERT_NOT_NULL(sideWidget(), "Side Widget is NULL!", return false; ) ;
00273   return sideWidget()->isVisible();
00274 }
00275 
00276 bool KLFShowHideSideWidgetManager::eventFilter(QObject *obj, QEvent *event)
00277 {
00278   if (sideWidget() != NULL) {
00279     KLF_ASSERT_CONDITION(ourParentWidget() == sideWidget()->parentWidget(),
00280                      "We have a side widget that is not a child of our 'parent widget' ! Correcting parent.",
00281                      setOurParentWidget(sideWidget()->parentWidget()); ) ;
00282     QWidget * pw = sideWidget()->parentWidget();
00283     if (pw != NULL && obj == pw) {
00284       if (event->type() == QEvent::Resize) {
00285         QResizeEvent *re = (QResizeEvent*) event;
00286         klfDbg("resize event, new size="<<re->size()<<", old size="<<re->oldSize()
00287                <<"; sidewidget->isvisible="<<sideWidget()->isVisible()) ;
00288         if (sideWidget()->isVisible() && !d->infunction) {
00289           // only relevant if we are visible and not ourselves resizing the widget
00290           klfDbg("Readjusting inner widget size") ;
00291           d->msize += re->size() - re->oldSize();
00292         }
00293       }
00294     }
00295     if (obj == sideWidget()) {
00296       if (event->type() == QEvent::ParentAboutToChange) {
00297         d->oldParent = sideWidget()->parentWidget();
00298       } else if (event->type() == QEvent::ParentChange) {
00299         setOurParentWidget(pw);
00300       }
00301     }
00302   }
00303   
00304   return KLFSideWidgetManagerBase::eventFilter(obj, event);
00305 }
00306 
00307 bool KLFShowHideSideWidgetManager::event(QEvent *event)
00308 {
00309   return KLFSideWidgetManagerBase::event(event);
00310 }
00311 
00312 
00313 void KLFShowHideSideWidgetManager::showSideWidget(bool show)
00314 {
00315   KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00316   
00317   KLF_ASSERT_NOT_NULL(sideWidget(), "Side Widget is NULL!", return; ) ;
00318 
00319   klfDbg("show="<<show<<", sideWidgetVisible()="<<sideWidgetVisible()) ;
00320 
00321   if (show == sideWidgetVisible())
00322     return;
00323 
00324   QWidget *pw = sideWidget()->parentWidget();
00325 
00326   KLF_ASSERT_NOT_NULL(pw, "Parent Widget of Side Widget is NULL!", return; ) ;
00327   KLF_ASSERT_CONDITION(ourParentWidget() == sideWidget()->parentWidget(),
00328                    "We have a side widget that is not a child of our 'parent widget' ! Correcting parent.",
00329                    setOurParentWidget(sideWidget()->parentWidget()); ) ;
00330 
00331   QSize newSize;
00332   if (show) {
00333     d->msize = pw->size();
00334     klfDbg("Store inner widget size as "<<d->msize) ;
00335     newSize = d->msize;
00336     if (d->orientation & Qt::Horizontal)
00337       newSize += QSize(d->calcSpacing + sideWidget()->sizeHint().width(), 0);
00338     if (d->orientation & Qt::Vertical)
00339       newSize += QSize(0, d->calcSpacing + sideWidget()->sizeHint().height());
00340   } else {
00341     newSize = d->msize;
00342   }
00343 
00344   klfDbg("sideWidget is "<<sideWidget()) ;
00345   sideWidget()->setVisible(show);
00346 
00347   d->infunction = true;
00348   klfDbg("newSize is "<<newSize<<"; d->msize is "<<d->msize) ;
00349   if (newSize.isValid()) {
00350     QMetaObject::invokeMethod(this, "resizeParentWidget", Qt::QueuedConnection, Q_ARG(QSize, newSize));
00351   }
00352   // will probably(?) emit sideWidgetShown _after_ we resized, which is possibly a more desirable behavior (?)
00353   QMetaObject::invokeMethod(this, "sideWidgetShown", Qt::QueuedConnection, Q_ARG(bool, show));
00354 }
00355 
00356 
00357 void KLFShowHideSideWidgetManager::resizeParentWidget(const QSize& size)
00358 {
00359   KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00360   klfDbg("size="<<size) ;
00361   QWidget * sw = sideWidget();
00362   KLF_ASSERT_NOT_NULL(sw, "Side Widget is NULL!", return; ) ;
00363   KLF_ASSERT_NOT_NULL(sw->parentWidget(), "Side Widget is NULL!", return; ) ;
00364 
00365   QWidget *window = sw->window();
00366   KLF_ASSERT_NOT_NULL(window, "hey, side-widget->window() is NULL!", return; ) ;
00367   QSize diffsize = size - sw->parentWidget()->size();
00368   QSize winsize = window->size() + diffsize;
00369   klfDbg("resizing window to "<<winsize) ;
00370   window->setFixedSize(winsize);
00371   window->setFixedSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
00372   d->infunction = false;
00373 }
00374 
00375 
00376 KLF_DEFINE_PROPERTY_GETSET(KLFShowHideSideWidgetManager, Qt::Orientation, orientation, Orientation) ;
00377 
00378 KLF_DEFINE_PROPERTY_GETSET(KLFShowHideSideWidgetManager, int, calcSpacing, CalcSpacing) ;
00379 
00380 
00381 // ------------------
00382 
00383 
00384 
00385 
00386 struct KLFContainerSideWidgetManagerPrivate
00387 {
00388   KLF_PRIVATE_HEAD(KLFContainerSideWidgetManager)
00389   {
00390     isdestroying = false;
00391     dwidget = NULL;
00392     dlayout = NULL;
00393     init_pw = init_sw = NULL;
00394     saved_pw = NULL;
00395     want_restore_saved = true;
00396   }
00397 
00398   bool isdestroying;
00399   QPointer<QWidget> dwidget;
00400   QStackedLayout *dlayout;
00401 
00402   QWidget *init_pw;
00403   QWidget *init_sw;
00404 
00405   QPointer<QWidget> saved_pw;
00406   bool want_restore_saved;
00407 
00408 
00409   void restore_saved_parent(QWidget *oldw)
00410   {
00411     KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00412     KLF_ASSERT_NOT_NULL(oldw, "oldw is NULL!", return; ) ;
00413     klfDbg("oldw="<<(void*)oldw<<"; saved_pw="<<(void*)saved_pw) ;
00414     klfDbg("oldw="<<oldw<<"; saved_pw="<<saved_pw) ;
00415 
00416     oldw->setParent(saved_pw);
00417 
00418     klfDbg("set parent.") ;
00419 
00420     if (saved_pw != NULL && saved_pw->layout() != NULL) {
00421       klfDbg("About to reinsert into layout "<<saved_pw->layout()) ;
00422       saved_pw->layout()->addWidget(oldw);
00423     }
00424 
00425   }
00426 };
00427 
00428 
00429 KLFContainerSideWidgetManager::KLFContainerSideWidgetManager(QWidget *parentWidget, QWidget *sideWidget,
00430                                                              QObject *parent)
00431   : KLFSideWidgetManagerBase(parentWidget, sideWidget, false, parent)
00432 {
00433   KLF_INIT_PRIVATE(KLFContainerSideWidgetManager) ;
00434 
00435   klfDbg("parentWidget="<<parentWidget<<", sideWidget="<<sideWidget) ;
00436   connect(parentWidget, SIGNAL(destroyed(QObject*)), this, SLOT(aWidgetDestroyed(QObject*)));
00437   d->init_pw = parentWidget;
00438   d->init_sw = sideWidget;
00439 }
00440 
00441 void KLFContainerSideWidgetManager::init()
00442 {
00443   d->dwidget = createContainerWidget(d->init_pw);
00444   connect(d->dwidget, SIGNAL(destroyed(QObject*)), this, SLOT(aWidgetDestroyed(QObject*)));
00445 
00446   d->dwidget->installEventFilter(this); // intercept close events
00447 
00448   KLF_ASSERT_NOT_NULL(d->dwidget, "Created Container Widget is NULL!", return; ) ;
00449 
00450   d->dlayout = new QStackedLayout(d->dwidget);
00451   d->dlayout->setContentsMargins(0,0,0,0);
00452   d->dlayout->setSpacing(0);
00453 
00454   d->dwidget->hide();
00455 
00456   setOurParentWidget(d->init_pw);
00457   setSideWidget(d->init_sw);
00458 }
00459 
00460 
00461 QWidget * KLFContainerSideWidgetManager::containerWidget() const
00462 {
00463   return d->dwidget;
00464 }
00465 
00466 KLFContainerSideWidgetManager::~KLFContainerSideWidgetManager()
00467 {
00468   KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00469 
00470   d->isdestroying = true;
00471 
00472   if (d->dwidget != NULL) {
00473     // make sure we do not destroy the sideWidget while destroying d->dwidget !
00474     d->restore_saved_parent(sideWidget());
00475   }
00476 
00477   klfDbg("d->dwidget="<<(void*)d->dwidget) ;
00478   klfDbg("d->dwidget="<<d->dwidget) ;
00479 
00480 // d->dwidget should get deleted automatically when its parent widget dies.  Doing this
00481 // explicitly on the other hand, and in the destructor, may cause a segfault
00482 //
00483 //  if (d->dwidget != NULL) {
00484 //    klfDbg("deleting...") ;
00485 //    delete d->dwidget;
00486 //    klfDbg("...done") ;
00487 //  }
00488   KLF_DELETE_PRIVATE ;
00489 }
00490 
00491 bool KLFContainerSideWidgetManager::eventFilter(QObject *obj, QEvent *event)
00492 {
00493   if (obj == d->dwidget) {
00494     if (event->type() == QEvent::Close) {
00495       klfDbg("intercepting close event.") ;
00496       // close button clicked, eg. in floating window; make sure to hide the widget
00497       // appropriately, emitting sideWidgetShown(bool) too, or saving its geometry etc.
00498       showSideWidget(false);
00499       return true;
00500     }
00501   }
00502   return KLFSideWidgetManagerBase::eventFilter(obj, event);
00503 }
00504 
00505 bool KLFContainerSideWidgetManager::sideWidgetVisible() const
00506 {
00507   KLF_ASSERT_NOT_NULL(d->dwidget, "Container Widget is NULL! Did you forget to call init()?", return false; ) ;
00508   return d->dwidget->isVisible();
00509 }
00510 
00511 void KLFContainerSideWidgetManager::showSideWidget(bool show)
00512 {
00513   KLF_ASSERT_NOT_NULL(d->dwidget, "Container Widget is NULL! Did you forget to call init()?", return; ) ;
00514 
00515   // and actually show/hide the container widget
00516   d->dwidget->setVisible(show);
00517   d->dwidget->setFocus();
00518   emit sideWidgetShown(show);
00519 }
00520 
00521 // protected
00522 void KLFContainerSideWidgetManager::newSideWidgetSet(QWidget *oldw, QWidget *neww)
00523 {
00524   KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00525 
00526   if (d->isdestroying)
00527     return; // destructor has its own special treatment
00528 
00529   KLF_ASSERT_NOT_NULL(d->dwidget, "Container Widget is NULL! Did you forget to call init()?", return; ) ;
00530 
00531   klfDbg("new side widget: old="<<oldw<<", new="<<neww<<"; want restore saved="<<d->want_restore_saved) ;
00532   if (oldw != NULL && d->want_restore_saved) {
00533     d->dlayout->removeWidget(oldw);
00534     //    oldw->hide(); // setParent() automatically hides the widget
00535     d->restore_saved_parent(oldw);
00536   }
00537   if (d->saved_pw != NULL) {
00538     klfDbg("Disconnecting the saved parent widget.") ;
00539     disconnect(d->saved_pw, SIGNAL(destroyed()), this, 0);
00540     d->saved_pw = NULL;
00541   }
00542   if (neww != NULL) {
00543     d->saved_pw = neww->parentWidget(); // save its parent widget so that we can restore it
00544     if (d->saved_pw != NULL) {
00545       bool connected = connect(d->saved_pw, SIGNAL(destroyed(QObject*)), this, SLOT(aWidgetDestroyed(QObject*)));
00546       Q_UNUSED(connected) ;
00547       klfDbg("saving pw : "<<d->saved_pw<<" and connected to destroyed(QObject*) signal?="<<connected) ;
00548     }
00549     d->want_restore_saved = true;
00550     neww->setParent(NULL) ;
00551     neww->setParent(d->dwidget);
00552     d->dlayout->addWidget(neww);
00553     neww->show();
00554     emit sideWidgetShown(d->dwidget->isVisible());
00555   }
00556 }
00557 
00558 // protected
00559 void KLFContainerSideWidgetManager::newParentWidgetSet(QWidget *, QWidget *newWidget)
00560 {
00561   if (d->dwidget->parentWidget() != newWidget)
00562     d->dwidget->setParent(newWidget);
00563 }
00564 
00565 // private slot
00566 void KLFContainerSideWidgetManager::aWidgetDestroyed(QObject *w)
00567 {
00568   KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00569   klfDbg("w="<<w) ;
00570   if (w == d->saved_pw) {
00571     klfDbg("saved parent "<<d->saved_pw<<" or our own parent = "<<parent()<<" was destroyed!") ;
00572     d->want_restore_saved = false;
00573     d->saved_pw = NULL;
00574   }
00575   // else if (w == d->dwidget) {
00576   //    d->dwidget = NULL;
00577   //  }
00578 }
00579 
00580 
00581 
00582 
00583 
00584 
00585 
00586 
00587 // ------------------
00588 
00589 
00590 
00591 struct KLFFloatSideWidgetManagerPrivate
00592 {
00593   KLF_PRIVATE_HEAD(KLFFloatSideWidgetManager)
00594   {
00595     dwgeom = QRect();
00596   }
00597 
00598   QRect dwgeom;
00599 };
00600 
00601 
00602 KLFFloatSideWidgetManager::KLFFloatSideWidgetManager(QWidget *parentWidget, QWidget *sideWidget, QObject *parent)
00603   : KLFContainerSideWidgetManager(parentWidget, sideWidget, parent)
00604 {
00605   KLF_INIT_PRIVATE(KLFFloatSideWidgetManager) ;
00606 
00607   init();
00608 }
00609 
00610 KLFFloatSideWidgetManager::~KLFFloatSideWidgetManager()
00611 {
00612   KLF_DELETE_PRIVATE ;
00613 }
00614 
00615 Qt::WindowFlags KLFFloatSideWidgetManager::wflags() const
00616 {
00617   return containerWidget()->windowFlags();
00618 }
00619 
00620 bool KLFFloatSideWidgetManager::sideWidgetVisible() const
00621 {
00622   return containerWidget()->isVisible();
00623 }
00624 
00625 QWidget * KLFFloatSideWidgetManager::createContainerWidget(QWidget *pw)
00626 {
00627   return  new QWidget(pw, Qt::Tool|Qt::CustomizeWindowHint|Qt::WindowTitleHint
00628                       |Qt::WindowSystemMenuHint
00629 #if QT_VERSION >= 0x040500
00630                       |Qt::WindowCloseButtonHint
00631 #endif
00632                       );
00633 }
00634 
00635 void KLFFloatSideWidgetManager::setWFlags(Qt::WindowFlags wf)
00636 {
00637   KLF_ASSERT_NOT_NULL(sideWidget(), "side widget is NULL!", return; ) ;
00638   wf |= Qt::Window; // make sure it is not a nested widget
00639   containerWidget()->setWindowFlags(wf);
00640 }
00641 
00642 void KLFFloatSideWidgetManager::showSideWidget(bool show)
00643 {
00644   QWidget *w = containerWidget();
00645   if (sideWidgetVisible()) {
00646     // save position and size
00647     d->dwgeom = w->geometry();
00648   }
00649   if (show && d->dwgeom.isValid()) {
00650     // set saved position and size
00651     w->setGeometry(d->dwgeom);
00652   }
00653   // this automatically emits sideWidgetShown
00654   KLFContainerSideWidgetManager::showSideWidget(show);
00655 }
00656 
00657 
00658 // protected
00659 void KLFFloatSideWidgetManager::newSideWidgetSet(QWidget *oldw, QWidget *neww)
00660 {
00661   KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00662   KLFContainerSideWidgetManager::newSideWidgetSet(oldw, neww);
00663 }
00664 
00665 
00666 
00667 
00668 
00669 
00670 // ---------------------------------------------
00671 
00672 // static
00673 KLFFactoryManager KLFSideWidgetManagerFactory::pFactoryManager;
00674 
00675 
00676 KLFSideWidgetManagerFactory::KLFSideWidgetManagerFactory()
00677   : KLFFactoryBase(&pFactoryManager)
00678 {
00679 }
00680 KLFSideWidgetManagerFactory::~KLFSideWidgetManagerFactory()
00681 {
00682 }
00683 
00684 // static
00685 QStringList KLFSideWidgetManagerFactory::allSupportedTypes()
00686 {
00687   return pFactoryManager.allSupportedTypes();
00688 }
00689 
00690 // static
00691 KLFSideWidgetManagerFactory * KLFSideWidgetManagerFactory::findFactoryFor(const QString& managertype)
00692 {
00693   return dynamic_cast<KLFSideWidgetManagerFactory*>(pFactoryManager.findFactoryFor(managertype));
00694 }
00695 
00696 // static
00697 KLFSideWidgetManagerBase *
00698 /* */ KLFSideWidgetManagerFactory::findCreateSideWidgetManager(const QString& type, QWidget *pw,
00699                                                                QWidget *sw, QObject *parent)
00700 {
00701   KLFSideWidgetManagerFactory * f = findFactoryFor(type);
00702 
00703   KLF_ASSERT_NOT_NULL(f, "Can't find factory for side widget manager type="<<type<<"!", return NULL; ) ;
00704 
00705   return f->createSideWidgetManager(type, pw, sw, parent);
00706 }
00707 
00708 
00709 QStringList KLFSideWidgetManagerFactory::supportedTypes() const
00710 {
00711   return QStringList()
00712     << QLatin1String("ShowHide")
00713     << QLatin1String("Float")
00714 #ifdef KLF_WS_MAC
00715     << QLatin1String("Drawer")
00716 #endif
00717     ;
00718 }
00719 
00720 QString KLFSideWidgetManagerFactory::getTitleFor(const QString& type) const
00721 {
00722   if (type == QLatin1String("ShowHide"))
00723     return QObject::tr("Expand/Shrink Window", "[[KLFSideWidgetManagerFactory]]");
00724   if (type == QLatin1String("Float"))
00725     return QObject::tr("Floating Tool Window", "[[KLFSideWidgetManagerFactory]]");
00726   if (type == QLatin1String("Drawer"))
00727     return QObject::tr("Side Drawer", "[[KLFSideWidgetManagerFactory]]");
00728 
00729   return QString();
00730 }
00731 
00732 KLFSideWidgetManagerBase *
00733 /* */  KLFSideWidgetManagerFactory::createSideWidgetManager(const QString& type, QWidget *parentWidget,
00734                                                             QWidget *sideWidget, QObject *parent)
00735 {
00736   if (type == QLatin1String("ShowHide")) {
00737     return new KLFShowHideSideWidgetManager(parentWidget, sideWidget, parent);
00738   }
00739   if (type == QLatin1String("Float")) {
00740     return new KLFFloatSideWidgetManager(parentWidget, sideWidget, parent);
00741   }
00742 #ifdef KLF_WS_MAC
00743   if (type == QLatin1String("Drawer")) {
00744     return new KLFDrawerSideWidgetManager(parentWidget, sideWidget, parent);
00745   }
00746 #endif
00747 
00748   qWarning()<<KLF_FUNC_NAME<<": Unknown side-widget-manager type "<<type;
00749 
00750   return NULL;
00751 }
00752 
00753 
00754 // an instance of the factory
00755 KLFSideWidgetManagerFactory __klf_side_widget_manager_factory;
00756 
00757 
00758 // ---------
00759 
00760 struct KLFSideWidgetPrivate
00761 {
00762   KLF_PRIVATE_HEAD(KLFSideWidget) {
00763     manager = NULL;
00764     swmtype = QString();
00765   }
00766 
00767   KLFSideWidgetManagerBase * manager;
00768   QString swmtype;
00769 };
00770 
00771 
00772 /*KLFSideWidget::KLFSideWidget(SideWidgetManager mtype, QWidget *parent)
00773   : QWidget(parent)
00774 {
00775   KLF_INIT_PRIVATE(KLFSideWidget) ;
00776   setSideWidgetManager(mtype);
00777 }
00778 KLFSideWidget::KLFSideWidget(const QString& mtype, QWidget *parent)
00779 {
00780   KLF_INIT_PRIVATE(KLFSideWidget) ;
00781   setSideWidgetManager(mtype);
00782   }*/
00783 
00784 KLFSideWidget::KLFSideWidget(QWidget *parent)
00785   : QWidget(parent)
00786 {
00787   KLF_INIT_PRIVATE(KLFSideWidget) ;
00788 
00789   _inqtdesigner = false;
00790 
00791 }
00792 KLFSideWidget::~KLFSideWidget()
00793 {
00794   KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00795 
00796   delete d->manager;
00797 
00798   KLF_DELETE_PRIVATE ;
00799 }
00800 
00801 KLFSideWidgetManagerBase * KLFSideWidget::sideWidgetManager()
00802 {
00803   return d->manager;
00804 }
00805 
00806 bool KLFSideWidget::sideWidgetVisible() const
00807 {
00808   KLF_ASSERT_NOT_NULL(d->manager, "Manager is NULL!", return false) ;
00809   return d->manager->sideWidgetVisible();
00810 }
00811 
00812 QString KLFSideWidget::sideWidgetManagerType() const
00813 {
00814   return d->swmtype;
00815 }
00816 
00817 
00818 void KLFSideWidget::setSideWidgetManager(SideWidgetManager mtype)
00819 {
00820   QString s;
00821   switch (mtype) {
00822   case ShowHide: s = QLatin1String("ShowHide"); break;
00823   case Float: s = QLatin1String("Float"); break;
00824   case Drawer: s = QLatin1String("Drawer"); break;
00825   default: break;
00826   }
00827 
00828   KLF_ASSERT_CONDITION(!s.isEmpty(), "Invalid mtype: "<<mtype<<"!", return; ) ;
00829   setSideWidgetManager(s);
00830 }
00831 void KLFSideWidget::setSideWidgetManager(const QString& mtype)
00832 {
00833   KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00834 
00835   if (_inqtdesigner) {
00836     klfDbg("We're in Qt Designer. DUMMY ACTION.") ;
00837     d->swmtype = mtype;
00838     return;
00839   }
00840 
00841   if (d->swmtype == mtype) {
00842     klfDbg("no-op");
00843     return;
00844   }
00845 
00846   if (d->manager != NULL) {
00847     d->manager->hideSideWidget();
00848     klfDbg("deleting current manager") ;
00849     //    d->manager->setSideWidget(NULL) ; // re-take the side widget
00850     delete d->manager;
00851     d->manager = NULL;
00852     d->swmtype = QString();
00853   }
00854 
00855   d->swmtype = mtype;
00856   d->manager = KLFSideWidgetManagerFactory::findCreateSideWidgetManager(mtype, parentWidget(), this, this);
00857   KLF_ASSERT_NOT_NULL(d->manager, "Factory returned NULL manager for type "<<mtype<<"!", return; ) ;
00858 
00859   connect(d->manager, SIGNAL(sideWidgetShown(bool)), this, SIGNAL(sideWidgetShown(bool)));
00860 
00861   emit sideWidgetManagerTypeChanged(mtype);
00862   emit sideWidgetShown(d->manager->sideWidgetVisible());
00863 }
00864 
00865 void KLFSideWidget::showSideWidget(bool show)
00866 {
00867   if (_inqtdesigner)
00868     return;
00869 
00870   KLF_ASSERT_NOT_NULL(d->manager, "Manager is NULL! For debugging purposes, I'm creating a 'float' manager !",
00871                       setSideWidgetManager(Float); ) ;
00872   KLF_ASSERT_NOT_NULL(d->manager, "Manager is NULL!", return; ) ;
00873   d->manager->showSideWidget(show);
00874 }
00875 
00876 void KLFSideWidget::debug_unlock_qtdesigner()
00877 {
00878   if (_inqtdesigner) {
00879     _inqtdesigner = false;
00880     setSideWidgetManager(sideWidgetManagerType());
00881   }
00882 }

Generated by doxygen 1.7.6.1. The KLatexFormula website is hosted on sourceforge.net