F:/KPlato/koffice/libs/kofficeui/Kolinewidthaction.cpp

Aller à la documentation de ce fichier.
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>
00023 #include <QPixmap>
00024 #include <QBitmap>
00025 #include <q3whatsthis.h>
00026 #include <qmenubar.h>
00027 #include <QLayout>
00028 #include <QLabel>
00029 //Added by qt3to4:
00030 #include <Q3GridLayout>
00031 
00032 #include <kmenu.h>
00033 #include <kapplication.h>
00034 #include <kdebug.h>
00035 #include <ktoolbar.h>
00036 
00037 #include <kiconloader.h>
00038 #include <klocale.h>
00039 
00040 #include <KoUnitWidgets.h>
00041 #include <KoGlobal.h>
00042 
00043 class KoLineWidthAction::KoLineWidthActionPrivate
00044 {
00045   public:
00046     KoLineWidthActionPrivate()
00047     {
00048       m_currentWidth = 1.0;
00049       m_unit = KoUnit::U_PT;
00050     }
00051 
00052     ~KoLineWidthActionPrivate()
00053     {
00054     }
00055 
00056     double m_currentWidth;
00057     KoUnit::Unit m_unit;
00058 };
00059 
00060 KoLineWidthAction::KoLineWidthAction(const QString &text, const QString& icon,
00061   QObject* parent, const char* name) : KoSelectAction(text, icon, parent, name)
00062 {
00063   d = new KoLineWidthActionPrivate;
00064 
00065   createMenu();
00066 }
00067 
00068 KoLineWidthAction::KoLineWidthAction(const QString &text, const QString& icon, const QObject* receiver,
00069   const char* slot, QObject* parent, const char* name) : KoSelectAction(text, icon, parent, name)
00070 {
00071   d = new KoLineWidthActionPrivate;
00072 
00073   createMenu();
00074 
00075   connect(this, SIGNAL(lineWidthChanged(double)), receiver, slot);
00076 }
00077 
00078 KoLineWidthAction::~KoLineWidthAction()
00079 {
00080   delete d;
00081 }
00082 
00083 void KoLineWidthAction::createMenu()
00084 {
00085   KMenu* popup = popupMenu();
00086   QBitmap mask;
00087   QPixmap pix(70, 21);
00088   QPainter p(&pix, popup);
00089   int cindex = 0;
00090   QPen pen;
00091 
00092   for(int i = 1; i <= 10; i++) {
00093     pix.fill(Qt::white);
00094     pen.setWidth(qRound(i * POINT_TO_INCH(static_cast<double>(KoGlobal::dpiY()))));
00095     p.setPen(pen);
00096     p.drawLine(0, 10, pix.width(), 10);
00097     mask = pix;
00098     pix.setMask(mask);
00099     popup->insertItem(pix,cindex++);
00100   }
00101 
00102   popup->insertSeparator(cindex++);
00103   popup->insertItem(i18n("&Custom..."), cindex++);
00104 }
00105 
00106 void KoLineWidthAction::execute(int index)
00107 {
00108   bool ok = false;
00109 
00110   if((index >= 0) && (index < 10)) {
00111     d->m_currentWidth = (double) index + 1.0;
00112     ok = true;
00113   } if(index == 11) { // Custom width dialog...
00114     KoLineWidthChooser dlg(qApp->activeWindow());
00115     dlg.setUnit(d->m_unit);
00116     dlg.setWidth(d->m_currentWidth);
00117 
00118     if(dlg.exec()) {
00119       d->m_currentWidth = dlg.width();
00120       ok = true;
00121     }
00122   }
00123 
00124   if(ok) {
00125     setCurrentSelection(index);
00126     emit lineWidthChanged(d->m_currentWidth);
00127   }
00128 }
00129 
00130 double KoLineWidthAction::currentWidth() const
00131 {
00132   return d->m_currentWidth;
00133 }
00134 
00135 void KoLineWidthAction::setCurrentWidth(double width)
00136 {
00137   d->m_currentWidth = width;
00138 
00139   // Check if it is a standard width...
00140   for(int i = 1; i <= 10; i++) {
00141     if(KoUnit::toPoint(width) == (double) i) {
00142       setCurrentSelection(i - 1);
00143       return;
00144     }
00145   }
00146 
00147   //Custom width...
00148   setCurrentSelection(11);
00149 }
00150 
00151 void KoLineWidthAction::setUnit(KoUnit::Unit unit)
00152 {
00153   d->m_unit = unit;
00154 }
00155 
00157 //
00158 // KoLineWidthChooser
00159 //
00160 
00161 class KoLineWidthChooser::KoLineWidthChooserPrivate
00162 {
00163   public:
00164     KoUnitDoubleSpinBox* m_lineWidthUSBox;
00165 };
00166 
00167 KoLineWidthChooser::KoLineWidthChooser(QWidget* parent, const char* name)
00168  : KDialogBase(parent, name, true, i18n("Custom Line Width"), Ok|Cancel, Ok)
00169 {
00170   d = new KoLineWidthChooserPrivate;
00171 
00172   // Create the ui
00173   QWidget* mainWidget = new QWidget(this);
00174   setMainWidget(mainWidget);
00175   Q3GridLayout* gl = new Q3GridLayout(mainWidget, 1, 2, KDialog::marginHint(), KDialog::spacingHint());
00176   QLabel* textLbl = new QLabel(i18n("Line width:"), mainWidget);
00177   d->m_lineWidthUSBox = new KoUnitDoubleSpinBox(mainWidget, 0.0, 1000.0, 0.1, 1.0, KoUnit::U_PT, 2);
00178   gl->addWidget(textLbl, 0, 0);
00179   gl->addWidget(d->m_lineWidthUSBox, 0, 1);
00180 }
00181 
00182 KoLineWidthChooser::~KoLineWidthChooser()
00183 {
00184   delete d;
00185 }
00186 
00187 double KoLineWidthChooser::width() const
00188 {
00189   return d->m_lineWidthUSBox->value();
00190 }
00191 
00192 void KoLineWidthChooser::setUnit(KoUnit::Unit unit)
00193 {
00194   d->m_lineWidthUSBox->setUnit(unit);
00195 }
00196 
00197 void KoLineWidthChooser::setWidth(double width)
00198 {
00199   d->m_lineWidthUSBox->changeValue(width);
00200 }
00201 
00202 #include "Kolinewidthaction.moc"

Généré le Wed Nov 22 23:41:05 2006 pour KPlato par  doxygen 1.5.1-p1