summaryrefslogtreecommitdiffstats
path: root/lib/kofficeui/KoToolBox.cpp
blob: d6af0e97c68cccb5c04cbb8b4c71a33f8187a032 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
   Copyright (c) 2005 Boudewijn Rempt <boud@valdyas.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include <tqbuttongroup.h>
#include <tqnamespace.h>
#include <tqtoolbutton.h>
#include <tqlabel.h>
#include <tqtooltip.h>
#include <tqlayout.h>
#include <tqpixmap.h>
#include <tqtoolbar.h>
#include <tqdoctwindow.h>

#include <kdebug.h>
#include <kparts/event.h>
#include <klocale.h>
#include <ktoolbar.h>
#include <kiconloader.h>
#include <kseparator.h>
#include <kaction.h>
#include <kactioncollection.h>
#include <kactionclasses.h>

#include <KoMainWindow.h>
#include "KoToolBox.h"

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

KoToolBox::KoToolBox( KMainWindow *mainWin, const char* name, KInstance* instance, int numberOfTooltypes )
    : KToolBar( mainWin, TQt::DockLeft, false, name, true, true), m_instance(instance)
{
    setFullSize( false );
    setMargin(2);

    m_buttonGroup = new TQButtonGroup( 0L );
    m_buttonGroup->setExclusive( true );
    connect( m_buttonGroup, TQT_SIGNAL( pressed( int ) ), this, TQT_SLOT( slotButtonPressed( int ) ) );

    m_tools.setAutoDelete( true );
    // Create separate lists for the various sorts of tools
    for (int i = 0; i < numberOfTooltypes ; ++i) {
        ToolList * tl = new ToolList();
        m_tools.append(tl);
    }
}

KoToolBox::~KoToolBox()
{
    delete m_buttonGroup;
}


void KoToolBox::slotPressButton( int id )
{
    m_buttonGroup->setButton( id );
    slotButtonPressed( id );
}

void KoToolBox::slotButtonPressed( int id )
{
    if( id != m_buttonGroup->selectedId() && m_buttonGroup->selected() ) {
        m_buttonGroup->selected()->setDown( false );
    }
    m_idToActionMap.at(id)->activate();

}

void KoToolBox::registerTool( KAction *tool, int toolType, TQ_UINT32 priority )
{
    uint prio = priority;
    ToolList * tl = m_tools.at(toolType);
    while( (*tl)[prio] ) prio++;
    (*tl)[prio] = tool;
}

TQToolButton *KoToolBox::createButton(TQWidget * parent,  const char* iconName, TQString tooltip)
{
    TQToolButton *button = new TQToolButton(parent);

    if ( iconName && *iconName ) {
        TQPixmap pixmap = BarIcon( iconName, m_instance );
        button->setPixmap( pixmap );
        button->setToggleButton( true );
    }

    if ( !tooltip.isEmpty() ) {
        TQToolTip::add( button, tooltip );
    }
    return button;
}


void KoToolBox::setupTools()
{
    int id = 0;
    // Loop through tooltypes
    for (uint i = 0; i < m_tools.count(); ++i) {
        ToolList * tl = m_tools.at(i);

        if (!tl) continue;
        if (tl->isEmpty()) continue;

        ToolArea *tools = new ToolArea( this );
        ToolList::Iterator it;
        for ( it = tl->begin(); it != tl->end(); ++it )
        {
            KAction *tool = it.data();
            if(! tool)
                continue;
            TQToolButton *bn = createButton(tools->getNextParent(), tool->icon().latin1(), tool->toolTip());
            tools->add(bn);
            m_buttonGroup->insert( bn, id++ );
            m_idToActionMap.append( tool );
        }
        if (i < m_tools.count() -1) addSeparator();
        m_toolBoxes.append(tools);
    }
    // select first (select tool)
    m_buttonGroup->setButton( 0 );
    m_numberOfButtons = id;
}


void KoToolBox::setOrientation ( Qt::Orientation o )
{
    if ( barPos() == Floating ) { // when floating, make it a standing toolbox.
        o = o == Qt::Vertical ? Qt::Horizontal : Qt::Vertical;
    }

    TQDockWindow::setOrientation( o );

    for (uint i = 0; i < m_toolBoxes.count(); ++i) {
        ToolArea *t = m_toolBoxes.at(i);
        t->setOrientation(o);
    }
}


void KoToolBox::enableTools(bool enable)
{
    if (m_tools.isEmpty()) return;
    if (!m_buttonGroup) return;
    if (m_numberOfButtons <= 0) return;

    for (uint i = 0; i < m_tools.count(); ++i) {
        ToolList * tl = m_tools.at(i);

        if (!tl) continue;

        ToolList::Iterator it;
        for ( it = tl->begin(); it != tl->end(); ++it )
            if (it != 0 && it.data())
                it.data()->setEnabled(enable);
    }
    m_buttonGroup->setEnabled(enable);
    for (TQ_UINT32 i = 0; i < m_numberOfButtons; ++i) {
        m_buttonGroup->find( i )->setEnabled( enable );
    }
}

void KoToolBox::slotSetTool(const TQString & toolname)
{
    for (uint i = 0; i < m_idToActionMap.count(); ++i) {
        KAction * a = m_idToActionMap.at(i);
        if (!a || a->name() != toolname) continue;

        m_buttonGroup->setButton(i);
        return;

    }
}


// ----------------------------------------------------------------
//                         class ToolArea


ToolArea::ToolArea(TQWidget *parent)
    : TQWidget(parent), m_left(true)
{
    m_layout = new TQBoxLayout(this, TQBoxLayout::LeftToRight, 0, 0, 0);
    TQWidget *w = new TQWidget(this);
    m_layout->addWidget(w);

    TQGridLayout *grid = new TQGridLayout(w, 2, 2);
    m_leftRow = new TQWidget(w);
    grid->addWidget(m_leftRow, 0, 0);
    m_leftLayout = new TQBoxLayout(m_leftRow, TQBoxLayout::TopToBottom, 0, 1, 0);

    w = new TQWidget(this);
    m_layout->addWidget(w);

    grid = new TQGridLayout(w, 2, 2);
    m_rightRow = new TQWidget(w);
    grid->addWidget(m_rightRow, 0, 0);
    m_rightLayout = new TQBoxLayout(m_rightRow, TQBoxLayout::TopToBottom, 0, 1, 0);
}


ToolArea::~ToolArea()
{
}


void ToolArea::add(TQWidget *button)
{
    if (m_left)
        m_leftLayout->addWidget(button);
    else
        m_rightLayout->addWidget(button);
    button->show();
    m_left = !m_left;
}


TQWidget* ToolArea::getNextParent()
{
    if (m_left)
        return m_leftRow;
    return m_rightRow;
}


void ToolArea::setOrientation ( Qt::Orientation o )
{
    TQBoxLayout::Direction  dir = (o != Qt::Horizontal 
				  ? TQBoxLayout::TopToBottom
				  : TQBoxLayout::LeftToRight);
    m_leftLayout->setDirection(dir);
    m_rightLayout->setDirection(dir);

    m_layout->setDirection(o == Qt::Horizontal
			   ? TQBoxLayout::TopToBottom
			   : TQBoxLayout::LeftToRight);
}


#include "KoToolBox.moc"