MDIView.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2007 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 
00026 #ifndef _PreComp_
00027 # include <qapplication.h>
00028 # include <qregexp.h>
00029 # include <QEvent>
00030 # include <QCloseEvent>
00031 # include <QMdiSubWindow>
00032 #endif
00033 
00034 
00035 #include "MDIView.h"
00036 #include "Command.h"
00037 #include "Document.h"
00038 #include "Application.h"
00039 #include "MainWindow.h"
00040 
00041 using namespace Gui;
00042 
00043 TYPESYSTEM_SOURCE_ABSTRACT(Gui::MDIView,Gui::BaseView);
00044 
00045 
00046 MDIView::MDIView(Gui::Document* pcDocument,QWidget* parent, Qt::WFlags wflags)
00047   : QMainWindow(parent, wflags), BaseView(pcDocument),currentMode(Child), wstate(Qt::WindowNoState)
00048 {
00049     setAttribute(Qt::WA_DeleteOnClose);
00050 }
00051 
00052 MDIView::~MDIView()
00053 {
00054     //This view might be the focus widget of the main window. In this case we must
00055     //clear the focus and e.g. set the focus directly to the main window, otherwise
00056     //the application crashes when accessing this deleted view.
00057     //This effect only occurs if this widget is not in Child mode, because otherwise
00058     //the focus stuff is done correctly.
00059     QWidget* foc = getMainWindow()->focusWidget();
00060     if (foc) {
00061         QWidget* par = foc;
00062         while (par) {
00063             if (par == this) {
00064                 getMainWindow()->setFocus();
00065                 break;
00066             }
00067             par = par->parentWidget();
00068         }
00069     }
00070 }
00071 
00072 void MDIView::deleteSelf()
00073 {
00074     // When using QMdiArea make sure to remove the QMdiSubWindow
00075     // this view is associated with.
00076 #if !defined (NO_USE_QT_MDI_AREA)
00077     QWidget* parent = this->parentWidget();
00078     if (qobject_cast<QMdiSubWindow*>(parent))
00079         delete parent;
00080     else
00081 #endif
00082         delete this;
00083 }
00084 
00085 void MDIView::onRelabel(Gui::Document *pDoc)
00086 {
00087     if (!bIsPassive) {
00088         // Try to separate document name and view number if there is one
00089         QString cap = windowTitle();
00090         // Either with dirty flag ...
00091         QRegExp rx(QLatin1String("(\\s\\:\\s\\d+\\[\\*\\])$"));
00092         int pos = rx.lastIndexIn(cap);
00093         if (pos == -1) {
00094             // ... or not
00095             rx.setPattern(QLatin1String("(\\s\\:\\s\\d+)$"));
00096             pos = rx.lastIndexIn(cap);
00097         }
00098         if (pos != -1) {
00099             cap = QString::fromUtf8(pDoc->getDocument()->Label.getValue());
00100             cap += rx.cap();
00101             setWindowTitle(cap);
00102         }
00103         else {
00104             cap = QString::fromUtf8(pDoc->getDocument()->Label.getValue());
00105             cap = QString::fromAscii("%1[*]").arg(cap);
00106             setWindowTitle(cap);
00107         }
00108     }
00109 }
00110 
00111 void MDIView::viewAll()
00112 {
00113 }
00114 
00116 bool MDIView::onMsg(const char* pMsg,const char** ppReturn)
00117 {
00118     return false;
00119 }
00120 
00121 bool MDIView::onHasMsg(const char* pMsg) const
00122 {
00123     return false;
00124 }
00125 
00126 bool MDIView::canClose(void)
00127 {
00128     if (!bIsPassive && getGuiDocument() && getGuiDocument()->isLastView()) {
00129         this->setFocus(); // raises the view to front
00130         return (getGuiDocument()->canClose());
00131     }
00132 
00133     return true;
00134 }
00135 
00136 void MDIView::closeEvent(QCloseEvent *e)
00137 {
00138     if (canClose()) {
00139         e->accept();
00140         if (!bIsPassive) {
00141             // must be detached so that the last view can get asked
00142             Document* doc = this->getGuiDocument();
00143             if (doc && !doc->isLastView())
00144                 doc->detachView(this);
00145         }
00146 
00147         // Note: When using QMdiArea we must not use removeWindow()
00148         // because otherwise the QMdiSubWindow will loose its parent
00149         // and thus the notification in QMdiSubWindow::closeEvent of
00150         // other mdi windows to get maximized if this window
00151         // is maximized will fail.
00152         // This odd behaviour is caused by the invocation of 
00153         // d->mdiArea->removeSubWindow(parent) which we must let there
00154         // because otherwise other parts don't work as they should.
00155 #if defined (NO_USE_QT_MDI_AREA)
00156         // avoid flickering
00157         getMainWindow()->removeWindow(this);
00158 #endif
00159         QMainWindow::closeEvent(e);
00160     }
00161     else
00162         e->ignore();
00163 }
00164 
00165 void MDIView::windowStateChanged( MDIView* )
00166 {
00167 }
00168 
00169 void MDIView::print()
00170 {
00171     // print command specified but print method not overriden!
00172     assert(0);
00173 }
00174 
00175 void MDIView::printPdf()
00176 {
00177     // print command specified but print method not overriden!
00178     assert(0);
00179 }
00180 
00181 void MDIView::printPreview()
00182 {
00183     // print command specified but print method not overriden!
00184     assert(0);
00185 }
00186 
00187 QSize MDIView::minimumSizeHint () const
00188 {
00189     return QSize(400, 300);
00190 }
00191 
00192 void MDIView::changeEvent(QEvent *e)
00193 {
00194     switch (e->type()) {
00195         case QEvent::ActivationChange:
00196             {
00197                 // Forces this top-level window to be the active view of the main window
00198                 if (isActiveWindow()) {
00199                     if (getMainWindow()->activeWindow() != this)
00200                         getMainWindow()->setActiveWindow(this);
00201                 }
00202             }   break;
00203         case QEvent::WindowTitleChange:
00204         case QEvent::ModifiedChange:
00205             {
00206                 // sets the appropriate tab of the tabbar
00207                 getMainWindow()->tabChanged(this);
00208             }   break;
00209         default:
00210             {
00211                 QMainWindow::changeEvent(e);
00212             }   break;
00213     }
00214 }
00215 
00216 #if defined(Q_WS_X11)
00217 // To fix bug #0000345 move function declaration to here
00218 extern void qt_x11_wait_for_window_manager( QWidget* w ); // defined in qwidget_x11.cpp
00219 #endif
00220 
00221 void MDIView::setCurrentViewMode(ViewMode mode)
00222 {
00223     switch (mode) {
00224         // go to normal mode
00225         case Child:
00226             {
00227                 if (this->currentMode == FullScreen) {
00228                     showNormal();
00229                     setWindowFlags(windowFlags() & ~Qt::Window);
00230                 }
00231                 else if (this->currentMode == TopLevel) {
00232                     this->wstate = windowState();
00233                     setWindowFlags( windowFlags() & ~Qt::Window );
00234                 }
00235 
00236                 if (this->currentMode != Child) {
00237                     this->currentMode = Child;
00238                     getMainWindow()->addWindow(this);
00239                     getMainWindow()->activateWindow();
00240                     update();
00241                 }
00242             }   break;
00243         // go to top-level mode
00244         case TopLevel:
00245             {
00246                 if (this->currentMode == Child) {
00247 #if !defined (NO_USE_QT_MDI_AREA)
00248                     if (qobject_cast<QMdiSubWindow*>(this->parentWidget()))
00249 #endif
00250                         getMainWindow()->removeWindow(this);
00251                     setWindowFlags(windowFlags() | Qt::Window);
00252                     setParent(0, Qt::Window | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | 
00253                                  Qt::WindowMinMaxButtonsHint);
00254                     if (this->wstate & Qt::WindowMaximized)
00255                         showMaximized();
00256                     else
00257                         showNormal();
00258 
00259 #if defined(Q_WS_X11)
00260                     //extern void qt_x11_wait_for_window_manager( QWidget* w ); // defined in qwidget_x11.cpp
00261                     qt_x11_wait_for_window_manager(this);
00262 #endif
00263                     activateWindow();
00264                 }
00265                 else if (this->currentMode == FullScreen) {
00266                     if (this->wstate & Qt::WindowMaximized)
00267                         showMaximized();
00268                     else
00269                         showNormal();
00270                 }
00271             
00272                 this->currentMode = TopLevel;
00273                 update();
00274             }   break;
00275         // go to fullscreen mode
00276         case FullScreen:
00277             {
00278                 if (this->currentMode == Child) {
00279 #if !defined (NO_USE_QT_MDI_AREA)
00280                     if (qobject_cast<QMdiSubWindow*>(this->parentWidget()))
00281 #endif
00282                         getMainWindow()->removeWindow(this);
00283                     setWindowFlags(windowFlags() | Qt::Window);
00284                     setParent(0, Qt::Window);
00285                     showFullScreen();
00286                 }
00287                 else if (this->currentMode == TopLevel) {
00288                     this->wstate = windowState();
00289                     showFullScreen();
00290                 }
00291                 
00292                 this->currentMode = FullScreen;
00293                 update();
00294             }   break;
00295     }
00296 }
00297 
00298 #include "moc_MDIView.cpp"

Generated on Wed Nov 23 19:00:21 2011 for FreeCAD by  doxygen 1.6.1