Back to index

scribus-ng  1.3.4.dfsg+svn20071115
scrpalettebase.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 /***************************************************************************
00008  *   Copyright (C) 2005 by Craig Bradney                                   *
00009  *   cbradney@scribus.info                                                 *
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  *   This program is distributed in the hope that it will be useful,       *
00017  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00018  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00019  *   GNU General Public License for more details.                          *
00020  *                                                                         *
00021  *   You should have received a copy of the GNU General Public License     *
00022  *   along with this program; if not, write to the                         *
00023  *   Free Software Foundation, Inc.,                                       *
00024  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00025  ***************************************************************************/
00026 
00027 #include <qapplication.h>
00028 #include <qpoint.h>
00029 
00030 #include "scrpalettebase.h"
00031 #include "scrpalettebase.moc"
00032 #include "scribus.h"
00033 #include "prefsmanager.h"
00034 #include "prefsfile.h"
00035 #include "prefscontext.h"
00036 #include "util.h"
00037 
00038 
00039 ScrPaletteBase::ScrPaletteBase(  QWidget * parent, const char * name, bool modal, WFlags f) : QDialog ( parent, name, modal, f | Qt::WStyle_Customize | Qt::WStyle_Tool | Qt::WStyle_Title | Qt::WStyle_MinMax | Qt::WStyle_SysMenu | Qt::WStyle_NormalBorder),
00040 palettePrefs(0),
00041 prefsContextName(QString::null),
00042 visibleOnStartup(false)
00043 {
00044        originalParent=parent;
00045        tempParent=0;
00046        setIcon(loadIcon("AppIcon.png"));
00047        setPrefsContext(name);
00048        connect(PrefsManager::instance(), SIGNAL(prefsChanged()), this, SLOT(setFontSize()));
00049 }
00050 
00051 void ScrPaletteBase::setPrefsContext(QString context)
00052 {
00053        if (prefsContextName.isEmpty())
00054        {
00055               prefsContextName=context;
00056               if (!prefsContextName.isEmpty())
00057               {
00058                      palettePrefs = PrefsManager::instance()->prefsFile->getContext(prefsContextName);
00059                      if (palettePrefs)
00060                             visibleOnStartup = palettePrefs->getBool("visible");
00061               }
00062               else
00063                      palettePrefs = NULL;
00064        }
00065 }
00066 
00067 void ScrPaletteBase::startup()
00068 {
00069        setFontSize();
00070        setShown(visibleOnStartup);
00071        emit paletteShown(visibleOnStartup);
00072 }
00073 
00074 void ScrPaletteBase::setPaletteShown(bool visible)
00075 {
00076        storeVisibility(visible);
00077        setShown(visible);
00078        setActiveWindow();
00079 }
00080 
00081 void ScrPaletteBase::setFontSize()
00082 {
00083        QFont newfont(font());
00084        newfont.setPointSize(PrefsManager::instance()->appPrefs.PaletteFontSize);
00085        setFont(newfont);
00086 }
00087 
00088 /*
00089 void ScrPaletteBase::keyPressEvent(QKeyEvent *keyEvent)
00090 {
00091        // Sample code to use in overriding classes 
00092        int keyMod;
00093        switch (keyEvent->state())
00094        {
00095               case ShiftButton:
00096                      keyMod = SHIFT;
00097                      break;
00098               case AltButton:
00099                      keyMod = ALT;
00100                      break;
00101               case ControlButton:
00102                      keyMod = CTRL;
00103                      break;
00104               default:
00105                      keyMod = 0;
00106                      break;
00107        }
00108        // Tell our action to be off
00109        //     if (keyMod==0 && keyEvent->key()==Key_Escape)
00110        //     emit paletteShown(false);
00111 
00112        QDialog::keyPressEvent(keyEvent);
00113 }
00114        */
00115 void ScrPaletteBase::closeEvent(QCloseEvent *closeEvent)
00116 {
00117        emit paletteShown(false);
00118        closeEvent->ignore();
00119        hide();
00120 }
00121 
00122 void ScrPaletteBase::hideEvent(QHideEvent*)
00123 {
00124        storePosition();
00125        storeSize();
00126 }
00127 
00128 void ScrPaletteBase::show()
00129 {
00130        if (palettePrefs)
00131        {
00132               QDesktopWidget *d = QApplication::desktop();
00133               QSize gStrut = QApplication::globalStrut();
00134               if (palettePrefs->contains("left"))
00135               {
00136                      // all palettes should have enough room for 3x3 min widgets
00137                      int vwidth  = QMIN(QMAX(3*gStrut.width(), palettePrefs->getInt("width")),
00138                                         d->width());
00139                      int vheight = QMIN(QMAX(3*gStrut.height(), palettePrefs->getInt("height")),
00140                                         d->height());
00141                      // palettes should not use too much screen space
00142                      if (vwidth > d->width()/3 && vheight > d->height()/3)
00143                             vwidth = d->width()/3;
00144                      // and should be partly visible
00145                      int vleft   = QMIN(QMAX(-vwidth + gStrut.width(), palettePrefs->getInt("left")),
00146                                         d->width() - gStrut.width());
00147                      int vtop = QMIN(palettePrefs->getInt("top"), d->height() - gStrut.height());
00148 #if defined(QT_MAC) || defined(_WIN32)
00149                      // on Mac and Windows you're dead if the titlebar is not on screen
00150                      vtop    = QMAX(64, vtop);
00151 #else
00152                      vtop    = QMAX(-vheight + gStrut.height(), vtop);
00153 #endif
00154                      // Check values against current screen size
00155                      QRect scr = QApplication::desktop()->screen()->geometry();
00156                      if ( vleft >= scr.width() )
00157                             vleft = 0;
00158                      if ( vtop >= scr.height() )
00159                             vtop = 64;
00160                      if ( vwidth >= scr.width() )
00161                             vwidth = QMAX( gStrut.width(), scr.width() - vleft );
00162                      if ( vheight >= scr.height() )
00163                             vheight = QMAX( gStrut.height(), scr.height() - vtop );
00164 //                   qDebug(QString("root %1x%2 %7 palette %3x%4 @ (%5,%6)").arg(d->width()).arg(d->height())
00165 //                          .arg(vwidth).arg(vheight).arg(vleft).arg(vtop).arg(name()));
00166 //                   setGeometry(vleft, vtop, vwidth, vheight);
00167                      resize(vwidth, vheight);
00168                      move(vleft, vtop);
00169               }
00170               storeVisibility(true);
00171        }
00172        QDialog::show();
00173 }
00174 
00175 void ScrPaletteBase::hide()
00176 {
00177        if (isShown())
00178        {
00179               storePosition();
00180               storeSize();
00181               QDialog::hide();
00182        }
00183 }
00184 
00185 void ScrPaletteBase::reject()
00186 {
00187        emit paletteShown(false);
00188        QDialog::reject();
00189 }
00190 
00191 void ScrPaletteBase::storePosition()
00192 {
00193        if (palettePrefs)
00194        {
00195               QRect geo = frameGeometry();
00196               palettePrefs->set("left", geo.left());
00197               palettePrefs->set("top", geo.top());
00198        }
00199 }
00200 
00201 void ScrPaletteBase::storePosition(int newX, int newY)
00202 {
00203        if (palettePrefs)
00204        {
00205               palettePrefs->set("left", newX);
00206               palettePrefs->set("top", newY);
00207        }
00208 }
00209 
00210 void ScrPaletteBase::storeSize()
00211 {
00212        if (palettePrefs)
00213        {
00214               palettePrefs->set("width", width());
00215               palettePrefs->set("height", height());
00216        }
00217 }
00218 
00219 void ScrPaletteBase::storeVisibility(bool vis)
00220 {
00221        if (palettePrefs)
00222               palettePrefs->set("visible", vis);
00223 }
00224 
00225 int ScrPaletteBase::exec(QWidget* newParent)
00226 {
00227        Q_ASSERT(tempParent==0 && newParent!=0);
00228        tempParent=newParent;
00229        int wflags=getWFlags();
00230        reparent(newParent, wflags , QPoint(0,0), false);
00231        int i=QDialog::exec();
00232        reparent(originalParent, wflags , QPoint(0,0), false);
00233        tempParent=0;
00234        return i;
00235 }