lib

Kolinewidthaction.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 Peter Simonsson <psn@linux.se>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "Kolinewidthaction.h"
00021 
00022 #include <qpainter.h>
00023 #include <qpixmap.h>
00024 #include <qbitmap.h>
00025 #include <qwhatsthis.h>
00026 #include <qmenubar.h>
00027 #include <qlayout.h>
00028 #include <qlabel.h>
00029 
00030 #include <kpopupmenu.h>
00031 #include <kapplication.h>
00032 #include <kdebug.h>
00033 #include <ktoolbar.h>
00034 #include <ktoolbarbutton.h>
00035 #include <kiconloader.h>
00036 #include <klocale.h>
00037 
00038 #include <KoUnitWidgets.h>
00039 #include <KoGlobal.h>
00040 
00041 class KoLineWidthAction::KoLineWidthActionPrivate
00042 {
00043   public:
00044     KoLineWidthActionPrivate()
00045     {
00046       m_currentWidth = 1.0;
00047       m_unit = KoUnit::U_PT;
00048     }
00049 
00050     ~KoLineWidthActionPrivate()
00051     {
00052     }
00053 
00054     double m_currentWidth;
00055     KoUnit::Unit m_unit;
00056 };
00057 
00058 KoLineWidthAction::KoLineWidthAction(const QString &text, const QString& icon,
00059   QObject* parent, const char* name) : KoSelectAction(text, icon, parent, name)
00060 {
00061   d = new KoLineWidthActionPrivate;
00062 
00063   createMenu();
00064 }
00065 
00066 KoLineWidthAction::KoLineWidthAction(const QString &text, const QString& icon, const QObject* receiver,
00067   const char* slot, QObject* parent, const char* name) : KoSelectAction(text, icon, parent, name)
00068 {
00069   d = new KoLineWidthActionPrivate;
00070 
00071   createMenu();
00072 
00073   connect(this, SIGNAL(lineWidthChanged(double)), receiver, slot);
00074 }
00075 
00076 KoLineWidthAction::~KoLineWidthAction()
00077 {
00078   delete d;
00079 }
00080 
00081 void KoLineWidthAction::createMenu()
00082 {
00083   KPopupMenu* popup = popupMenu();
00084   QBitmap mask;
00085   QPixmap pix(70, 21);
00086   QPainter p(&pix, popup);
00087   int cindex = 0;
00088   QPen pen;
00089 
00090   for(int i = 1; i <= 10; i++) {
00091     pix.fill(white);
00092     pen.setWidth(qRound(i * POINT_TO_INCH(static_cast<double>(KoGlobal::dpiY()))));
00093     p.setPen(pen);
00094     p.drawLine(0, 10, pix.width(), 10);
00095     mask = pix;
00096     pix.setMask(mask);
00097     popup->insertItem(pix,cindex++);
00098   }
00099 
00100   popup->insertSeparator(cindex++);
00101   popup->insertItem(i18n("&Custom..."), cindex++);
00102 }
00103 
00104 void KoLineWidthAction::execute(int index)
00105 {
00106   bool ok = false;
00107 
00108   if((index >= 0) && (index < 10)) {
00109     d->m_currentWidth = (double) index + 1.0;
00110     ok = true;
00111   } if(index == 11) { // Custom width dialog...
00112     KoLineWidthChooser dlg(qApp->activeWindow());
00113     dlg.setUnit(d->m_unit);
00114     dlg.setWidth(d->m_currentWidth);
00115 
00116     if(dlg.exec()) {
00117       d->m_currentWidth = dlg.width();
00118       ok = true;
00119     }
00120   }
00121 
00122   if(ok) {
00123     setCurrentSelection(index);
00124     emit lineWidthChanged(d->m_currentWidth);
00125   }
00126 }
00127 
00128 double KoLineWidthAction::currentWidth() const
00129 {
00130   return d->m_currentWidth;
00131 }
00132 
00133 void KoLineWidthAction::setCurrentWidth(double width)
00134 {
00135   d->m_currentWidth = width;
00136 
00137   // Check if it is a standard width...
00138   for(int i = 1; i <= 10; i++) {
00139     if(KoUnit::toPoint(width) == (double) i) {
00140       setCurrentSelection(i - 1);
00141       return;
00142     }
00143   }
00144 
00145   //Custom width...
00146   setCurrentSelection(11);
00147 }
00148 
00149 void KoLineWidthAction::setUnit(KoUnit::Unit unit)
00150 {
00151   d->m_unit = unit;
00152 }
00153 
00155 //
00156 // KoLineWidthChooser
00157 //
00158 
00159 class KoLineWidthChooser::KoLineWidthChooserPrivate
00160 {
00161   public:
00162     KoUnitDoubleSpinBox* m_lineWidthUSBox;
00163 };
00164 
00165 KoLineWidthChooser::KoLineWidthChooser(QWidget* parent, const char* name)
00166  : KDialogBase(parent, name, true, i18n("Custom Line Width"), Ok|Cancel, Ok)
00167 {
00168   d = new KoLineWidthChooserPrivate;
00169 
00170   // Create the ui
00171   QWidget* mainWidget = new QWidget(this);
00172   setMainWidget(mainWidget);
00173   QGridLayout* gl = new QGridLayout(mainWidget, 1, 2, KDialog::marginHint(), KDialog::spacingHint());
00174   QLabel* textLbl = new QLabel(i18n("Line width:"), mainWidget);
00175   d->m_lineWidthUSBox = new KoUnitDoubleSpinBox(mainWidget, 0.0, 1000.0, 0.1, 1.0, KoUnit::U_PT, 2);
00176   gl->addWidget(textLbl, 0, 0);
00177   gl->addWidget(d->m_lineWidthUSBox, 0, 1);
00178 }
00179 
00180 KoLineWidthChooser::~KoLineWidthChooser()
00181 {
00182   delete d;
00183 }
00184 
00185 double KoLineWidthChooser::width() const
00186 {
00187   return d->m_lineWidthUSBox->value();
00188 }
00189 
00190 void KoLineWidthChooser::setUnit(KoUnit::Unit unit)
00191 {
00192   d->m_lineWidthUSBox->setUnit(unit);
00193 }
00194 
00195 void KoLineWidthChooser::setWidth(double width)
00196 {
00197   d->m_lineWidthUSBox->changeValue(width);
00198 }
00199 
00200 #include "Kolinewidthaction.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys