summaryrefslogtreecommitdiffstats
path: root/kdeui/tests/kcomboboxtest.cpp
blob: 2eb1f73e0b666438227f4a877b53ab6def595892 (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
#define protected public // for delegate()
#include <kcombobox.h>
#undef protected

#include "kcomboboxtest.h"

#include <assert.h>

#include <kapplication.h>
#include <kdebug.h>
#include <kdialog.h>
#include <klocale.h>
#include <kiconloader.h>
#include <ksimpleconfig.h>

#include <qpushbutton.h>
#include <qlayout.h>
#include <qpixmap.h>
#include <qlabel.h>
#include <qhbox.h>
#include <qtimer.h>


KComboBoxTest::KComboBoxTest(QWidget* widget, const char* name )
              :QWidget(widget, name)
{
  QVBoxLayout *vbox = new QVBoxLayout (this, KDialog::marginHint(), KDialog::spacingHint());
  
  // Test for KCombo's KLineEdit destruction
  KComboBox *testCombo = new KComboBox( true, this ); // rw, with KLineEdit
  testCombo->setEditable( false ); // destroys our KLineEdit
  assert( testCombo->delegate() == 0L );
  delete testCombo; // not needed anymore  
  
  // Qt combobox
  QHBox* hbox = new QHBox(this);
  hbox->setSpacing (KDialog::spacingHint());
  QLabel* lbl = new QLabel("&QCombobox:", hbox);
  lbl->setSizePolicy (QSizePolicy::Maximum, QSizePolicy::Preferred);
  
  m_qc = new QComboBox(hbox, "QtReadOnlyCombo" );
  lbl->setBuddy (m_qc);  
  QObject::connect (m_qc, SIGNAL(activated(int)), SLOT(slotActivated(int)));
  QObject::connect (m_qc, SIGNAL(activated(const QString&)), SLOT (slotActivated(const QString&)));
  vbox->addWidget (hbox);
  
  // Read-only combobox
  hbox = new QHBox(this);
  hbox->setSpacing (KDialog::spacingHint());
  lbl = new QLabel("&Read-Only Combo:", hbox);
  lbl->setSizePolicy (QSizePolicy::Maximum, QSizePolicy::Preferred);

  m_ro = new KComboBox(hbox, "ReadOnlyCombo" );
  lbl->setBuddy (m_ro);
  m_ro->setCompletionMode( KGlobalSettings::CompletionAuto );
  QObject::connect (m_ro, SIGNAL(activated(int)), SLOT(slotActivated(int)));
  QObject::connect (m_ro, SIGNAL(activated(const QString&)), SLOT (slotActivated(const QString&)));
  vbox->addWidget (hbox);

  // Read-write combobox
  hbox = new QHBox(this);
  hbox->setSpacing (KDialog::spacingHint());
  lbl = new QLabel("&Editable Combo:", hbox);
  lbl->setSizePolicy (QSizePolicy::Maximum, QSizePolicy::Preferred);

  m_rw = new KComboBox( true, hbox, "ReadWriteCombo" );
  lbl->setBuddy (m_rw);
  m_rw->setDuplicatesEnabled( true );
  m_rw->setInsertionPolicy( QComboBox::NoInsertion );
  m_rw->setTrapReturnKey( true );
  QObject::connect (m_rw, SIGNAL(activated(int)), SLOT(slotActivated(int)));
  QObject::connect (m_rw, SIGNAL(activated(const QString&)), SLOT(slotActivated(const QString&)));
  QObject::connect (m_rw, SIGNAL(returnPressed()), SLOT(slotReturnPressed()));
  QObject::connect (m_rw, SIGNAL(returnPressed(const QString&)), SLOT(slotReturnPressed(const QString&)));
  vbox->addWidget (hbox);

  // History combobox...
  hbox = new QHBox(this);
  hbox->setSpacing (KDialog::spacingHint());
  lbl = new QLabel("&History Combo:", hbox);
  lbl->setSizePolicy (QSizePolicy::Maximum, QSizePolicy::Preferred);

  m_hc = new KHistoryCombo( true, hbox, "HistoryCombo" );
  lbl->setBuddy (m_hc);
  m_hc->setDuplicatesEnabled( true );
  m_hc->setInsertionPolicy( QComboBox::NoInsertion );
  QObject::connect (m_hc, SIGNAL(activated(int)), SLOT(slotActivated(int)));
  QObject::connect (m_hc, SIGNAL(activated(const QString&)), SLOT(slotActivated(const QString&)));
  QObject::connect (m_hc, SIGNAL(returnPressed()), SLOT(slotReturnPressed()));  
  vbox->addWidget (hbox);
  m_hc->setTrapReturnKey(true);

  // Read-write combobox that is a replica of code in konqueror...
  hbox = new QHBox(this);
  hbox->setSpacing (KDialog::spacingHint());
  lbl = new QLabel( "&Konq's Combo:", hbox);
  lbl->setSizePolicy (QSizePolicy::Maximum, QSizePolicy::Preferred);

  m_konqc = new KComboBox( true, hbox, "KonqyCombo" );
  lbl->setBuddy (m_konqc);
  m_konqc->setMaxCount( 10 );
  QObject::connect (m_konqc, SIGNAL(activated(int)), SLOT(slotActivated(int)));
  QObject::connect (m_konqc, SIGNAL(activated(const QString&)), SLOT (slotActivated(const QString&)));
  QObject::connect (m_konqc, SIGNAL(returnPressed()), SLOT(slotReturnPressed()));
  vbox->addWidget (hbox);

  // Create an exit button
  hbox = new QHBox (this);
  m_btnExit = new QPushButton( "E&xit", hbox );
  QObject::connect( m_btnExit, SIGNAL(clicked()), SLOT(quitApp()) );

  // Create a disable button...
  m_btnEnable = new QPushButton( "Disa&ble", hbox );
  QObject::connect (m_btnEnable, SIGNAL(clicked()), SLOT(slotDisable()));

  vbox->addWidget (hbox);

  // Popuplate the select-only list box
  QStringList list;
  list << "Stone" << "Tree" << "Peables" << "Ocean" << "Sand" << "Chips"
       << "Computer" << "Mankind";
  list.sort();
  
  // Setup the qcombobox
  m_qc->insertStringList (list);
  
  // Setup read-only combo
  m_ro->insertStringList( list );
  m_ro->completionObject()->setItems( list );

  // Setup read-write combo
  m_rw->insertStringList( list );
  m_rw->completionObject()->setItems( list );

  // Setup read-write combo
  m_hc->insertStringList( list );
  m_hc->completionObject()->setItems( list );

  // Setup konq's combobox
  KSimpleConfig historyConfig( "konq_history" );
  historyConfig.setGroup( "Location Bar" );
  KCompletion * s_pCompletion = new KCompletion;
  s_pCompletion->setOrder( KCompletion::Weighted );
  s_pCompletion->setItems( historyConfig.readListEntry( "ComboContents" ) );
  s_pCompletion->setCompletionMode( KGlobalSettings::completionMode() );
  m_konqc->setCompletionObject( s_pCompletion );

  QPixmap pix = SmallIcon("www");
  m_konqc->insertItem( pix, "http://www.kde.org" );
  m_konqc->setCurrentItem( m_konqc->count()-1 );

  m_timer = new QTimer (this);
  connect (m_timer, SIGNAL (timeout()), SLOT (slotTimeout()));
}

KComboBoxTest::~KComboBoxTest()
{
  if (m_timer)
  {
    delete m_timer;
    m_timer = 0;
  }
}

void KComboBoxTest::slotDisable ()
{
  if (m_timer->isActive())
    return;

  m_btnEnable->setEnabled (!m_btnEnable->isEnabled());

  m_timer->start (5000, true);
}

void KComboBoxTest::slotTimeout ()
{
  bool enabled = m_ro->isEnabled();

  if (enabled)
    m_btnEnable->setText ("Ena&ble");
  else
    m_btnEnable->setText ("Disa&ble");
  
  m_qc->setEnabled (!enabled);  
  m_ro->setEnabled (!enabled);
  m_rw->setEnabled (!enabled);
  m_hc->setEnabled (!enabled);
  m_konqc->setEnabled (!enabled);

  m_btnEnable->setEnabled (!m_btnEnable->isEnabled());
}

void KComboBoxTest::slotActivated( int index )
{
  kdDebug() << "Activated Combo: " << sender()->name() << ", index:" << index << endl;
}

void KComboBoxTest::slotActivated (const QString& item)
{
  kdDebug() << "Activated Combo: " << sender()->name() << ", item: " << item << endl;
}

void KComboBoxTest::slotReturnPressed ()
{
  kdDebug() << "Return Pressed: " << sender()->name() << endl;
}

void KComboBoxTest::slotReturnPressed(const QString& item)
{
  kdDebug() << "Return Pressed, value = " << item << endl;
}

void KComboBoxTest::quitApp()
{
  kapp->closeAllWindows();
}

int main ( int argc, char **argv)
{
  KApplication a(argc, argv, "kcomboboxtest");
  KComboBoxTest* t= new KComboBoxTest;
  a.setMainWidget (t);
  t->show ();
  return a.exec();
}

#include "kcomboboxtest.moc"