lib

Kolinewidthaction.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 Peter Simonsson <psn@linux.se>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "Kolinewidthaction.h"
21
22#include <tqpainter.h>
23#include <tqpixmap.h>
24#include <tqbitmap.h>
25#include <tqwhatsthis.h>
26#include <tqmenubar.h>
27#include <tqlayout.h>
28#include <tqlabel.h>
29
30#include <tdepopupmenu.h>
31#include <tdeapplication.h>
32#include <kdebug.h>
33#include <tdetoolbar.h>
34#include <tdetoolbarbutton.h>
35#include <kiconloader.h>
36#include <tdelocale.h>
37
38#include <KoUnitWidgets.h>
39#include <KoGlobal.h>
40
41class KoLineWidthAction::KoLineWidthActionPrivate
42{
43 public:
44 KoLineWidthActionPrivate()
45 {
46 m_currentWidth = 1.0;
47 m_unit = KoUnit::U_PT;
48 }
49
50 ~KoLineWidthActionPrivate()
51 {
52 }
53
54 double m_currentWidth;
55 KoUnit::Unit m_unit;
56};
57
58KoLineWidthAction::KoLineWidthAction(const TQString &text, const TQString& icon,
59 TQObject* parent, const char* name) : KoSelectAction(text, icon, parent, name)
60{
61 d = new KoLineWidthActionPrivate;
62
63 createMenu();
64}
65
66KoLineWidthAction::KoLineWidthAction(const TQString &text, const TQString& icon, const TQObject* receiver,
67 const char* slot, TQObject* parent, const char* name) : KoSelectAction(text, icon, parent, name)
68{
69 d = new KoLineWidthActionPrivate;
70
71 createMenu();
72
73 connect(this, TQ_SIGNAL(lineWidthChanged(double)), receiver, slot);
74}
75
76KoLineWidthAction::~KoLineWidthAction()
77{
78 delete d;
79}
80
82{
84 TQBitmap mask;
85 TQPixmap pix(70, 21);
86 TQPainter p(&pix, popup);
87 int cindex = 0;
88 TQPen pen;
89
90 for(int i = 1; i <= 10; i++) {
91 pix.fill(white);
92 pen.setWidth(tqRound(i * POINT_TO_INCH(static_cast<double>(KoGlobal::dpiY()))));
93 p.setPen(pen);
94 p.drawLine(0, 10, pix.width(), 10);
95 mask = pix;
96 pix.setMask(mask);
97 popup->insertItem(pix,cindex++);
98 }
99
100 popup->insertSeparator(cindex++);
101 popup->insertItem(i18n("&Custom..."), cindex++);
102}
103
105{
106 bool ok = false;
107
108 if((index >= 0) && (index < 10)) {
109 d->m_currentWidth = (double) index + 1.0;
110 ok = true;
111 } if(index == 11) { // Custom width dialog...
112 KoLineWidthChooser dlg(tqApp->activeWindow());
113 dlg.setUnit(d->m_unit);
114 dlg.setWidth(d->m_currentWidth);
115
116 if(dlg.exec()) {
117 d->m_currentWidth = dlg.width();
118 ok = true;
119 }
120 }
121
122 if(ok) {
123 setCurrentSelection(index);
124 emit lineWidthChanged(d->m_currentWidth);
125 }
126}
127
129{
130 return d->m_currentWidth;
131}
132
134{
135 d->m_currentWidth = width;
136
137 // Check if it is a standard width...
138 for(int i = 1; i <= 10; i++) {
139 if(KoUnit::toPoint(width) == (double) i) {
140 setCurrentSelection(i - 1);
141 return;
142 }
143 }
144
145 //Custom width...
147}
148
150{
151 d->m_unit = unit;
152}
153
155//
156// KoLineWidthChooser
157//
158
159class KoLineWidthChooser::KoLineWidthChooserPrivate
160{
161 public:
162 KoUnitDoubleSpinBox* m_lineWidthUSBox;
163};
164
165KoLineWidthChooser::KoLineWidthChooser(TQWidget* parent, const char* name)
166 : KDialogBase(parent, name, true, i18n("Custom Line Width"), Ok|Cancel, Ok)
167{
168 d = new KoLineWidthChooserPrivate;
169
170 // Create the ui
171 TQWidget* mainWidget = new TQWidget(this);
172 setMainWidget(mainWidget);
173 TQGridLayout* gl = new TQGridLayout(mainWidget, 1, 2, KDialog::marginHint(), KDialog::spacingHint());
174 TQLabel* textLbl = new TQLabel(i18n("Line width:"), mainWidget);
175 d->m_lineWidthUSBox = new KoUnitDoubleSpinBox(mainWidget, 0.0, 1000.0, 0.1, 1.0, KoUnit::U_PT, 2);
176 gl->addWidget(textLbl, 0, 0);
177 gl->addWidget(d->m_lineWidthUSBox, 0, 1);
178}
179
180KoLineWidthChooser::~KoLineWidthChooser()
181{
182 delete d;
183}
184
186{
187 return d->m_lineWidthUSBox->value();
188}
189
191{
192 d->m_lineWidthUSBox->setUnit(unit);
193}
194
196{
197 d->m_lineWidthUSBox->changeValue(width);
198}
199
200#include "Kolinewidthaction.moc"
static int marginHint()
static int spacingHint()
void execute(int index)
Reimplemented from KoSelectAction.
void createMenu()
Draws and adds each item of the menu.
void setCurrentWidth(double width)
Set the current width.
KoLineWidthAction(const TQString &text, const TQString &icon, TQObject *parent=0, const char *name=0)
Constructs a KoLineWidthAction with a text and an icon.
double currentWidth() const
Returns the currently selected line width.
void setUnit(KoUnit::Unit unit)
Set which unit to use in the custom width dialog.
void lineWidthChanged(double)
Emited when a new line width have been selected.
This class provides a dialog for setting a custom line width.
void setUnit(KoUnit::Unit unit)
Set unit to use when showing the width.
void setWidth(double width)
Set line width.
double width() const
Returns the selected line width in points.
TDEPopupMenu * popupMenu() const
Returns a pointer to the popup menu.
KoSelectAction(const TQString &text, const TQString &icon, TQObject *parent=0, const char *name=0)
Constructs a KoSelectAction with a text and an icon.
void popup(const TQPoint &global)
Shows the popup menu.
virtual void setCurrentSelection(int index)
Set which item that should be selected.
Spin box for double precision numbers with unit display.
Unit
Length units supported by KOffice.
Definition KoUnit.h:63
static double toPoint(double ptValue)
Prepare ptValue to be displayed in pt.
Definition KoUnit.h:77
virtual TQString text() const
const char * name(StdAction id)