Back to index

scribus-ng  1.3.4.dfsg+svn20071115
sctoolbar.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) 2006 by Riku Leino                                      *
00009  *   riku@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 "sctoolbar.h"
00028 #include "sctoolbar.moc"
00029 #include "prefscontext.h"
00030 #include "prefsfile.h"
00031 #include "prefsmanager.h"
00032 #include <qevent.h>
00033 #include <qdockarea.h>
00034 #include <qdockwindow.h>
00035 #include <qmainwindow.h>
00036 #include <qstring.h>
00037 #include <qtoolbutton.h>
00038 #include <qpopupmenu.h>
00039 
00040 extern QPixmap loadIcon(QString nam);
00041 
00042 ScToolBar::ScToolBar(const QString& name, const QString &prefName, QMainWindow *parent, QDockWindow::Orientation o)
00043 : QToolBar(name, parent),
00044   m_name(QString("ToolBar-%1").arg(prefName)),
00045   m_prefs(PrefsManager::instance()->prefsFile->getContext(m_name))
00046 {
00047        hide();
00048        setCloseMode(QDockWindow::Undocked);
00049 
00050        if (m_prefs->contains("IsDocked")) // set docking
00051        {
00052               if (m_prefs->getBool("IsDocked", true)) {
00053                      QString dockPlace = m_prefs->get("DockPlace", "top");
00054                      if (dockPlace == "top")
00055                             mainWindow()->addDockWindow(this, Qt::DockTop);
00056                      else if (dockPlace == "bottom")
00057                             mainWindow()->addDockWindow(this, Qt::DockBottom);
00058                      else if (dockPlace == "right")
00059                             mainWindow()->addDockWindow(this, Qt::DockRight);
00060                      else if (dockPlace == "left")
00061                             mainWindow()->addDockWindow(this, Qt::DockLeft);
00062               } else {
00063                      undock();
00064                      move(QPoint(m_prefs->getInt("x", 50), m_prefs->getInt("y", 50)));
00065               }
00066        }
00067 
00068        if (m_prefs->contains("FloatOrientation")) // set float orientation
00069        {
00070               floatOrientation = m_prefs->getInt("FloatOrientation", 1) == Hor ?
00071                                  QDockWindow::Horizontal : QDockWindow::Vertical;
00072        }
00073        else
00074        {
00075               floatOrientation = o;
00076               m_prefs->set("FloatOrientation", o == QDockWindow::Horizontal ? Hor : Vert);
00077        }
00078 
00079        if (!m_prefs->contains("PosIndex"))
00080               storeDockPosition();
00081        else
00082               moveDocks();
00083 
00084        dockTop = m_prefs->getBool("DockTop", 1);
00085        dockRight = m_prefs->getBool("DockRight", 1);
00086        dockBottom = m_prefs->getBool("DockBottom", 1);
00087        dockLeft = m_prefs->getBool("DockLeft", 1);
00088        mainWindow()->setDockEnabled(this, DockTop, dockTop);
00089        mainWindow()->setDockEnabled(this, DockRight, dockRight);
00090        mainWindow()->setDockEnabled(this, DockBottom, dockBottom);
00091        mainWindow()->setDockEnabled(this, DockLeft, dockLeft);
00092 
00093        if (place() == InDock)
00094               setOrientation(area()->orientation());
00095        else
00096               setOrientation(floatOrientation);
00097 
00098        initPrefsButton();
00099        languageChange();
00100 
00101        connect(this, SIGNAL(placeChanged(QDockWindow::Place)),
00102                this, SLOT(slotPlaceChanged(QDockWindow::Place)));
00103 }
00104 
00105 void ScToolBar::initVisibility()
00106 {
00107        if (m_prefs->getBool("IsVisible", true))
00108        {
00109               show();
00110               if (place() == InDock)
00111                      setOrientation(area()->orientation());
00112               else
00113                      setOrientation(floatOrientation);
00114        }
00115        else
00116               hide();
00117        connect(this, SIGNAL(visibilityChanged(bool)), this, SLOT(slotVisibilityChanged(bool)));
00118 }
00119 
00120 int ScToolBar::position()
00121 {
00122        if (place() == QDockWindow::OutsideDock)
00123               return -1;
00124 
00125        int index = -1;
00126        area()->hasDockWindow(this, &index);
00127 
00128        return index;
00129 }
00130 
00131 void ScToolBar::storeDockPosition()
00132 {
00133        m_prefs->set("PosIndex", position());
00134        m_prefs->set("Offset", offset());
00135 }
00136 
00137 void ScToolBar::storeDockPositions()
00138 {
00139        if (place() == QDockWindow::OutsideDock)
00140               return; // nothing to store if not docked
00141        QPtrList<QDockWindow> tbs = area()->dockWindowList();
00142        for (uint i = 0; i < tbs.count(); ++i)
00143        {
00144               if (ScToolBar *sctb = dynamic_cast<ScToolBar*>(tbs.at(i)))
00145                      sctb->storeDockPosition();
00146        }
00147 }
00148 
00149 void ScToolBar::moveDocks()
00150 {
00151        if (place() == QDockWindow::OutsideDock)
00152               return; // do not move if not docked;
00153        QPtrList<QDockWindow> tbs = area()->dockWindowList();
00154        for (uint i = 0; i < tbs.count(); ++i)
00155        {
00156               if (ScToolBar *sctb = dynamic_cast<ScToolBar*>(tbs.at(i)))
00157                      sctb->moveDock();
00158        }
00159 }
00160 
00161 void ScToolBar::moveDock()
00162 {
00163        if (place() == QDockWindow::OutsideDock)
00164               return; // do not move if not docked
00165        area()->moveDockWindow(this, m_prefs->getInt("PosIndex", -1));
00166        setOffset(m_prefs->getInt("Offset", 0));
00167 }
00168 
00169 void ScToolBar::slotPlaceChanged(QDockWindow::Place p)
00170 {
00171        m_prefs->set("IsDocked", p == QDockWindow::InDock);
00172        m_prefs->set("x", x());
00173        m_prefs->set("y", y());
00174        if (p == QDockWindow::InDock) {
00175               setOrientation(area()->orientation());
00176               QString dockPlace = "top";
00177               if (area() == mainWindow()->leftDock())
00178                      dockPlace = "left";
00179               else if (area() == mainWindow()->rightDock())
00180                      dockPlace = "right";
00181               else if (area() == mainWindow()->bottomDock())
00182                      dockPlace = "bottom";
00183 
00184               m_prefs->set("DockPlace", dockPlace);
00185               storeDockPositions();
00186        } else {
00187               setOrientation(floatOrientation);
00188        }
00189 }
00190 
00191 void ScToolBar::slotVisibilityChanged(bool visible)
00192 {
00193        m_prefs->set("IsVisible", visible);
00194 }
00195 
00196 void ScToolBar::slotTop()
00197 {
00198        dockTop = !dockTop;
00199        mainWindow()->setDockEnabled(this, DockTop, dockTop);
00200        dockMenu->setItemChecked(dockMenu->idAt(0), dockTop);
00201        m_prefs->set("DockTop", dockTop);
00202        if (place() == InDock && mainWindow()->topDock() == area())
00203               undock();
00204 }
00205 
00206 void ScToolBar::slotRight()
00207 {
00208        dockRight = !dockRight;
00209        mainWindow()->setDockEnabled(this, DockRight, dockRight);
00210        dockMenu->setItemChecked(dockMenu->idAt(1), dockRight);
00211        m_prefs->set("DockRight", dockRight);
00212        if (place() == InDock && mainWindow()->rightDock() == area())
00213               undock();
00214 }
00215 
00216 void ScToolBar::slotBottom()
00217 {
00218        dockBottom = !dockBottom;
00219        mainWindow()->setDockEnabled(this, DockBottom, dockBottom);
00220        dockMenu->setItemChecked(dockMenu->idAt(2), dockBottom);
00221        m_prefs->set("DockBottom", dockBottom);
00222        if (place() == InDock && mainWindow()->bottomDock() == area())
00223               undock();
00224 }
00225 
00226 void ScToolBar::slotLeft()
00227 {
00228        dockLeft = !dockLeft;
00229        mainWindow()->setDockEnabled(this, DockLeft, dockLeft);
00230        dockMenu->setItemChecked(dockMenu->idAt(3), dockLeft);
00231        m_prefs->set("DockLeft", dockLeft);
00232        if (place() == InDock && mainWindow()->leftDock() == area())
00233               undock();
00234 }
00235 
00236 void ScToolBar::slotVert()
00237 {
00238        floatOrientation = QDockWindow::Vertical;
00239        orientationMenu->setItemChecked(orientationMenu->idAt(0), false);
00240        orientationMenu->setItemChecked(orientationMenu->idAt(1), true);
00241        m_prefs->set("FloatOrientation", Vert);
00242        if (place() != InDock)
00243               setOrientation(floatOrientation);
00244 }
00245 
00246 void ScToolBar::slotHor()
00247 {
00248        floatOrientation = QDockWindow::Horizontal;
00249        orientationMenu->setItemChecked(orientationMenu->idAt(0), true);
00250        orientationMenu->setItemChecked(orientationMenu->idAt(1), false);
00251        m_prefs->set("FloatOrientation", Hor);
00252        if (place() != InDock)
00253               setOrientation(floatOrientation);
00254 }
00255 
00256 void ScToolBar::languageChange()
00257 {
00258        popup->clear();
00259 
00260        dockMenu = new QPopupMenu(0, "dockMenu");
00261        dockMenu->setCheckable(true);
00262        dockMenu->insertItem( tr("Top"), this, SLOT(slotTop()));
00263        dockMenu->insertItem( tr("Right"), this, SLOT(slotRight()));
00264        dockMenu->insertItem( tr("Bottom"), this, SLOT(slotBottom()));
00265        dockMenu->insertItem( tr("Left"), this, SLOT(slotLeft()));
00266        popup->insertItem( tr("Allow Docking To..."), dockMenu);
00267        dockMenu->setItemChecked(dockMenu->idAt(0), dockTop);
00268        dockMenu->setItemChecked(dockMenu->idAt(1), dockRight);
00269        dockMenu->setItemChecked(dockMenu->idAt(2), dockBottom);
00270        dockMenu->setItemChecked(dockMenu->idAt(3), dockLeft);
00271 
00272        orientationMenu = new QPopupMenu(0, "orientationMenu");
00273        orientationMenu->setCheckable(true);
00274        orientationMenu->insertItem( tr("Horizontal"), this, SLOT(slotHor()));
00275        orientationMenu->insertItem( tr("Vertical"), this, SLOT(slotVert()));
00276        popup->insertItem( tr("Floating Orientation..."), orientationMenu);
00277        if (floatOrientation == QDockWindow::Horizontal)
00278        {
00279               orientationMenu->setItemChecked(orientationMenu->idAt(0), true);
00280               orientationMenu->setItemChecked(orientationMenu->idAt(1), false);
00281        }
00282        else
00283        {
00284               orientationMenu->setItemChecked(orientationMenu->idAt(0), false);
00285               orientationMenu->setItemChecked(orientationMenu->idAt(1), true);
00286        }
00287 }
00288 
00289 void ScToolBar::initPrefsButton()
00290 {
00291        prefsButton = new QToolButton(Qt::DownArrow, this, "tbprefsbutton");
00292        prefsButton->setAutoRaise(true);
00293        popup = new QPopupMenu(0, "prefspopup");
00294        prefsButton->setPopup(popup);
00295        prefsButton->setPopupDelay(1);
00296 }
00297 
00298 ScToolBar::~ScToolBar()
00299 {
00300 
00301 }