Fix UI
[kdenetwork.git] / kdict / actions.cpp
blob5ca1aade334d1c35ed1bded1e0d715ac44f77a65
1 /* -------------------------------------------------------------
3 actions.cpp (part of The KDE Dictionary Client)
5 Copyright (C) 2000-2001 Christian Gebauer <gebauer@kde.org>
7 This file is distributed under the Artistic License.
8 See LICENSE for details.
10 -------------------------------------------------------------
12 DictComboAction, special KAction subclasses used
13 DictLabelAction, in the toolbar
14 DictButtonAction
16 ------------------------------------------------------------- */
18 #include "actions.h"
20 #include <qlabel.h>
21 #include <qpushbutton.h>
23 #include <kcombobox.h>
24 #include <ktoolbar.h>
27 DictComboAction::DictComboAction( const QString &text, QObject *parent, const char *name,
28 bool editable, bool autoSized )
29 : KAction( text, 0, parent, name ), m_editable(editable), m_autoSized(autoSized), m_compMode(KGlobalSettings::completionMode())
34 DictComboAction::~DictComboAction()
39 int DictComboAction::plug( QWidget *widget, int index )
41 if ( widget->inherits( "KToolBar" ) )
43 KToolBar* bar = static_cast<KToolBar*>( widget );
44 int id_ = KAction::getToolButtonID();
46 m_combo = new KComboBox(m_editable,bar);
47 m_combo->setCompletionMode(m_compMode);
49 bar->insertWidget( id_, m_combo->sizeHint().width(), m_combo, index );
50 bar->setItemAutoSized(id_,m_autoSized);
52 if ( m_combo ) {
53 connect(bar->getCombo(id_), SIGNAL(activated(const QString&)), SLOT(slotComboActivated(const QString&)));
54 connect(bar->getCombo(id_), SIGNAL(activated(int)), SLOT(slotComboActivated(int)));
56 if (m_editable)
57 m_combo->setInsertionPolicy( QComboBox::NoInsertion );
60 addContainer( bar, id_ );
61 connect( bar, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
62 return containerCount() - 1;
65 return -1;
69 void DictComboAction::unplug( QWidget *widget )
71 if ( widget->inherits( "KToolBar" ) )
73 KToolBar *bar = (KToolBar *)widget;
75 int idx = findContainer( bar );
77 if ( idx != -1 )
79 bar->removeItem( itemId( idx ) );
80 removeContainer( idx );
83 return;
88 QWidget* DictComboAction::widget()
90 return m_combo;
94 void DictComboAction::setFocus()
96 if (m_combo)
97 m_combo->setFocus();
101 QString DictComboAction::currentText() const
103 if (m_combo)
104 return m_combo->currentText();
105 else
106 return QString::null;
109 void DictComboAction::selectAll()
111 if (m_combo)
113 m_combo->lineEdit()->selectAll();
114 m_combo->lineEdit()->setFocus();
119 void DictComboAction::setEditText(const QString &s)
121 if (m_combo && m_editable)
122 m_combo->setEditText(s);
126 void DictComboAction::setCurrentItem(int index)
128 if (m_combo)
129 m_combo->setCurrentItem(index);
133 void DictComboAction::clearEdit()
135 if (m_combo && m_editable)
136 m_combo->clearEdit();
140 void DictComboAction::clear()
142 if (m_combo) {
143 m_combo->clear();
144 if (m_editable && m_combo->completionObject())
145 m_combo->completionObject()->clear();
150 void DictComboAction::setList(QStringList items)
152 if (m_combo) {
153 m_combo->clear();
154 m_combo->insertStringList(items);
155 if (m_editable && m_combo->completionObject())
156 m_combo->completionObject()->setItems(items);
157 if (!m_autoSized)
158 m_combo->setFixedWidth(m_combo->sizeHint().width());
163 KGlobalSettings::Completion DictComboAction::completionMode()
165 if (m_combo)
166 return m_combo->completionMode();
167 else
168 return m_compMode;
172 void DictComboAction::setCompletionMode(KGlobalSettings::Completion mode)
174 if (m_combo)
175 m_combo->setCompletionMode(mode);
176 else
177 m_compMode = mode;
181 void DictComboAction::slotComboActivated(int i)
183 emit(activated(i));
187 void DictComboAction::slotComboActivated(const QString &s)
189 emit(activated(s));
193 //*********************************************************************************
196 DictLabelAction::DictLabelAction( const QString &text, QObject *parent, const char *name )
197 : KAction( text, 0, parent, name )
202 DictLabelAction::~DictLabelAction()
207 int DictLabelAction::plug( QWidget *widget, int index )
209 if ( widget->inherits( "KToolBar" ) )
211 KToolBar *tb = (KToolBar *)widget;
213 int id = KAction::getToolButtonID();
215 QLabel *label = new QLabel( text(), widget, "kde toolbar widget" );
216 label->setMinimumWidth(label->sizeHint().width());
217 label->setBackgroundMode( Qt::PaletteButton );
218 label->setAlignment(AlignCenter | AlignVCenter);
219 label->adjustSize();
221 tb->insertWidget( id, label->width(), label, index );
223 addContainer( tb, id );
225 connect( tb, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
227 m_label = label;
229 return containerCount() - 1;
232 return -1;
236 void DictLabelAction::unplug( QWidget *widget )
238 if ( widget->inherits( "KToolBar" ) )
240 KToolBar *bar = (KToolBar *)widget;
242 int idx = findContainer( bar );
244 if ( idx != -1 )
246 bar->removeItem( itemId( idx ) );
247 removeContainer( idx );
250 return;
255 void DictLabelAction::setBuddy(QWidget *buddy)
257 if (m_label && buddy)
258 m_label->setBuddy(buddy);
262 //*********************************************************************************
265 DictButtonAction::DictButtonAction( const QString& text, QObject* receiver,
266 const char* slot, QObject* parent, const char* name )
267 : KAction( text, 0, receiver, slot, parent, name )
272 DictButtonAction::~DictButtonAction()
277 int DictButtonAction::plug( QWidget *widget, int index )
279 if ( widget->inherits( "KToolBar" ) )
281 KToolBar *tb = (KToolBar *)widget;
283 int id = KAction::getToolButtonID();
285 QPushButton *button = new QPushButton( text(), widget );
286 button->adjustSize();
287 connect(button,SIGNAL(clicked()),this,SLOT(activate()));
288 tb->insertWidget( id, button->width(), button, index );
290 addContainer( tb, id );
292 connect( tb, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
294 m_button = button;
296 return containerCount() - 1;
299 return -1;
303 void DictButtonAction::unplug( QWidget *widget )
305 if ( widget->inherits( "KToolBar" ) )
307 KToolBar *bar = (KToolBar *)widget;
309 int idx = findContainer( bar );
311 if ( idx != -1 )
313 bar->removeItem( itemId( idx ) );
314 removeContainer( idx );
320 int DictButtonAction::widthHint()
322 if (m_button)
323 return m_button->sizeHint().width();
324 else
325 return 0;
329 void DictButtonAction::setWidth(int width)
331 if (m_button)
332 m_button->setFixedWidth(width);
335 #include "actions.moc"