lib

KoToolBox.cpp
1/*
2 Copyright (c) 2005 Boudewijn Rempt <boud@valdyas.org>
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 <tqbuttongroup.h>
21#include <tqnamespace.h>
22#include <tqtoolbutton.h>
23#include <tqlabel.h>
24#include <tqtooltip.h>
25#include <tqlayout.h>
26#include <tqpixmap.h>
27#include <tqtoolbar.h>
28#include <tqdockwindow.h>
29
30#include <kdebug.h>
31#include <tdeparts/event.h>
32#include <tdelocale.h>
33#include <tdetoolbar.h>
34#include <kiconloader.h>
35#include <kseparator.h>
36#include <tdeaction.h>
37#include <tdeactioncollection.h>
38#include <tdeactionclasses.h>
39
40#include <KoMainWindow.h>
41#include "KoToolBox.h"
42
43#ifdef HAVE_CONFIG_H
44#include <config.h>
45#endif
46
47KoToolBox::KoToolBox( TDEMainWindow *mainWin, const char* name, TDEInstance* instance, int numberOfTooltypes )
48 : TDEToolBar( mainWin, TQt::DockLeft, false, name, true, true), m_instance(instance)
49{
50 setFullSize( false );
51 setMargin(2);
52
53 m_buttonGroup = new TQButtonGroup( 0L );
54 m_buttonGroup->setExclusive( true );
55 connect( m_buttonGroup, TQ_SIGNAL( pressed( int ) ), this, TQ_SLOT( slotButtonPressed( int ) ) );
56
57 m_tools.setAutoDelete( true );
58 // Create separate lists for the various sorts of tools
59 for (int i = 0; i < numberOfTooltypes ; ++i) {
60 ToolList * tl = new ToolList();
61 m_tools.append(tl);
62 }
63}
64
65KoToolBox::~KoToolBox()
66{
67 delete m_buttonGroup;
68}
69
70
71void KoToolBox::slotPressButton( int id )
72{
73 m_buttonGroup->setButton( id );
74 slotButtonPressed( id );
75}
76
77void KoToolBox::slotButtonPressed( int id )
78{
79 if( id != m_buttonGroup->selectedId() && m_buttonGroup->selected() ) {
80 m_buttonGroup->selected()->setDown( false );
81 }
82 m_idToActionMap.at(id)->activate();
83
84}
85
86void KoToolBox::registerTool( TDEAction *tool, int toolType, TQ_UINT32 priority )
87{
88 uint prio = priority;
89 ToolList * tl = m_tools.at(toolType);
90 while( (*tl)[prio] ) prio++;
91 (*tl)[prio] = tool;
92}
93
94TQToolButton *KoToolBox::createButton(TQWidget * parent, const char* iconName, TQString tooltip)
95{
96 TQToolButton *button = new TQToolButton(parent);
97
98 if ( iconName && *iconName ) {
99 TQPixmap pixmap = BarIcon( iconName, m_instance );
100 button->setPixmap( pixmap );
101 button->setToggleButton( true );
102 }
103
104 if ( !tooltip.isEmpty() ) {
105 TQToolTip::add( button, tooltip );
106 }
107 return button;
108}
109
110
111void KoToolBox::setupTools()
112{
113 int id = 0;
114 // Loop through tooltypes
115 for (uint i = 0; i < m_tools.count(); ++i) {
116 ToolList * tl = m_tools.at(i);
117
118 if (!tl) continue;
119 if (tl->isEmpty()) continue;
120
121 ToolArea *tools = new ToolArea( this );
122 ToolList::Iterator it;
123 for ( it = tl->begin(); it != tl->end(); ++it )
124 {
125 TDEAction *tool = it.data();
126 if(! tool)
127 continue;
128 TQToolButton *bn = createButton(tools->getNextParent(), tool->icon().latin1(), tool->toolTip());
129 tools->add(bn);
130 m_buttonGroup->insert( bn, id++ );
131 m_idToActionMap.append( tool );
132 }
133 if (i < m_tools.count() -1) addSeparator();
134 m_toolBoxes.append(tools);
135 }
136 // select first (select tool)
137 m_buttonGroup->setButton( 0 );
138 m_numberOfButtons = id;
139}
140
141
142void KoToolBox::setOrientation ( TQt::Orientation o )
143{
144 if ( barPos() == Floating ) { // when floating, make it a standing toolbox.
145 o = o == TQt::Vertical ? TQt::Horizontal : TQt::Vertical;
146 }
147
148 TQDockWindow::setOrientation( o );
149
150 for (uint i = 0; i < m_toolBoxes.count(); ++i) {
151 ToolArea *t = m_toolBoxes.at(i);
152 t->setOrientation(o);
153 }
154}
155
156
157void KoToolBox::enableTools(bool enable)
158{
159 if (m_tools.isEmpty()) return;
160 if (!m_buttonGroup) return;
161 if (m_numberOfButtons <= 0) return;
162
163 for (uint i = 0; i < m_tools.count(); ++i) {
164 ToolList * tl = m_tools.at(i);
165
166 if (!tl) continue;
167
168 ToolList::Iterator it;
169 for ( it = tl->begin(); it != tl->end(); ++it )
170 if (it != 0 && it.data())
171 it.data()->setEnabled(enable);
172 }
173 m_buttonGroup->setEnabled(enable);
174 for (TQ_UINT32 i = 0; i < m_numberOfButtons; ++i) {
175 m_buttonGroup->find( i )->setEnabled( enable );
176 }
177}
178
179void KoToolBox::slotSetTool(const TQString & toolname)
180{
181 for (uint i = 0; i < m_idToActionMap.count(); ++i) {
182 TDEAction * a = m_idToActionMap.at(i);
183 if (!a || a->name() != toolname) continue;
184
185 m_buttonGroup->setButton(i);
186 return;
187
188 }
189}
190
191
192// ----------------------------------------------------------------
193// class ToolArea
194
195
196ToolArea::ToolArea(TQWidget *parent)
197 : TQWidget(parent), m_left(true)
198{
199 m_layout = new TQBoxLayout(this, TQBoxLayout::LeftToRight, 0, 0, 0);
200 TQWidget *w = new TQWidget(this);
201 m_layout->addWidget(w);
202
203 TQGridLayout *grid = new TQGridLayout(w, 2, 2);
204 m_leftRow = new TQWidget(w);
205 grid->addWidget(m_leftRow, 0, 0);
206 m_leftLayout = new TQBoxLayout(m_leftRow, TQBoxLayout::TopToBottom, 0, 1, 0);
207
208 w = new TQWidget(this);
209 m_layout->addWidget(w);
210
211 grid = new TQGridLayout(w, 2, 2);
212 m_rightRow = new TQWidget(w);
213 grid->addWidget(m_rightRow, 0, 0);
214 m_rightLayout = new TQBoxLayout(m_rightRow, TQBoxLayout::TopToBottom, 0, 1, 0);
215}
216
217
218ToolArea::~ToolArea()
219{
220}
221
222
223void ToolArea::add(TQWidget *button)
224{
225 if (m_left)
226 m_leftLayout->addWidget(button);
227 else
228 m_rightLayout->addWidget(button);
229 button->show();
230 m_left = !m_left;
231}
232
233
234TQWidget* ToolArea::getNextParent()
235{
236 if (m_left)
237 return m_leftRow;
238 return m_rightRow;
239}
240
241
242void ToolArea::setOrientation ( TQt::Orientation o )
243{
244 TQBoxLayout::Direction dir = (o != TQt::Horizontal
245 ? TQBoxLayout::TopToBottom
246 : TQBoxLayout::LeftToRight);
247 m_leftLayout->setDirection(dir);
248 m_rightLayout->setDirection(dir);
249
250 m_layout->setDirection(o == TQt::Horizontal
251 ? TQBoxLayout::TopToBottom
252 : TQBoxLayout::LeftToRight);
253}
254
255
256#include "KoToolBox.moc"
virtual TQString toolTip() const
bool enable(BarStatus stat) TDE_DEPRECATED
BarPosition barPos() const
const char * name(StdAction id)