1/* This file is part of the KDE libraries
2 Copyright (C) 2000 David Faure <faure@kde.org>
3 Copyright (C) 2005 Hamish Rodda <rodda@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#include "klistdebugdialog.h"
22
23#include <kconfig.h>
24#include <kdebug.h>
25#include <klocale.h>
26#include <ktreewidgetsearchline.h>
27
28#include <QtDBus/QtDBus>
29#include <QLayout>
30#include <QTreeWidget>
31#include <QPushButton>
32
33KListDebugDialog::KListDebugDialog(const AreaMap& areaMap, QWidget *parent)
34 : KAbstractDebugDialog(parent)
35{
36 setCaption(i18n("Debug Settings"));
37 QWidget* mainWidget = new QWidget( this );
38 QVBoxLayout *lay = new QVBoxLayout( mainWidget );
39 lay->setMargin( KDialog::marginHint() );
40 lay->setSpacing( KDialog::spacingHint() );
41
42 m_incrSearch = new KTreeWidgetSearchLineWidget();
43 m_incrSearch->searchLine()->setClearButtonShown(true);
44 lay->addWidget( m_incrSearch );
45 // connect( m_incrSearch, SIGNAL( textChanged( const QString& ) ),
46 // SLOT( filterCheckBoxes( const QString& ) ) );
47
48 //@TODO: Change back to QListWidget once Trolltech fixed the task: #214420
49 //See http://trolltech.com/developer/task-tracker/index_html?id=214420&method=entry
50 m_areaWidget = new QTreeWidget();
51 m_areaWidget->setHeaderHidden(true);
52 m_areaWidget->setItemsExpandable(false);
53 m_areaWidget->setRootIsDecorated(false);
54 m_areaWidget->setUniformRowHeights(true);
55 lay->addWidget(m_areaWidget);
56
57 m_incrSearch->searchLine()->addTreeWidget(m_areaWidget);
58
59 for( QMap<QString,QString>::const_iterator it = areaMap.begin(); it != areaMap.end(); ++it ) {
60 QTreeWidgetItem* item = new QTreeWidgetItem(m_areaWidget, QStringList() << it.value());
61 item->setData(0, Qt::UserRole, it.key().simplified());
62 }
63
64 m_buttonContainer = new QWidget(mainWidget);
65 QHBoxLayout* selectButs = new QHBoxLayout(m_buttonContainer);
66 lay->addWidget(m_buttonContainer);
67 QPushButton* all = new QPushButton(i18n("&Select All"), m_buttonContainer);
68 QPushButton* none = new QPushButton(i18n("&Deselect All"), m_buttonContainer);
69 selectButs->addWidget( all );
70 selectButs->addWidget( none );
71
72 connect( all, SIGNAL( clicked() ), this, SLOT( selectAll() ) );
73 connect( none, SIGNAL( clicked() ), this, SLOT( deSelectAll() ) );
74
75 m_disableAll = new QCheckBox(mainWidget);
76 m_disableAll->setText(i18n("Disable all debug output"));
77 connect(m_disableAll, SIGNAL(toggled(bool)), this, SLOT(disableAllClicked()));
78 lay->addWidget(m_disableAll);
79
80 load();
81
82 buildButtons();
83 resize( 350, 400 );
84 setMainWidget( mainWidget );
85 m_incrSearch->searchLine()->setFocus();
86}
87
88void KListDebugDialog::selectAll()
89{
90 for (int i = 0; i < m_areaWidget->topLevelItemCount(); ++i) {
91 QTreeWidgetItem* item = m_areaWidget->topLevelItem(i);
92 if (!m_areaWidget->isItemHidden(item)) {
93 item->setCheckState(0, Qt::Checked);
94 }
95 }
96}
97
98void KListDebugDialog::deSelectAll()
99{
100 for (int i = 0; i < m_areaWidget->topLevelItemCount(); ++i) {
101 QTreeWidgetItem* item = m_areaWidget->topLevelItem(i);
102 if (!m_areaWidget->isItemHidden(item)) {
103 item->setCheckState(0, Qt::Unchecked);
104 }
105 }
106}
107
108void KListDebugDialog::doLoad()
109{
110 for (int i = 0; i < m_areaWidget->topLevelItemCount(); ++i) {
111 QTreeWidgetItem* item = m_areaWidget->topLevelItem(i);
112 KConfigGroup group = pConfig->group( item->data(0, Qt::UserRole).toByteArray() ); // Group name = debug area code = cb's name
113
114 int setting = group.readEntry("InfoOutput", -1);
115
116 switch (setting) {
117 case 4: // off
118 item->setCheckState(0, Qt::Unchecked);
119 break;
120 case -1: // default
121 case 2: //shell
122 item->setCheckState(0, Qt::Checked);
123 break;
124 case 3: //syslog
125 case 1: //msgbox
126 case 0: //file
127 default:
128 item->setCheckState(0, Qt::PartiallyChecked);
129 /////// Uses the triState capability of checkboxes
130 break;
131 }
132 }
133}
134
135void KListDebugDialog::doSave()
136{
137 for (int i = 0; i < m_areaWidget->topLevelItemCount(); ++i) {
138 QTreeWidgetItem* item = m_areaWidget->topLevelItem(i);
139 KConfigGroup group = pConfig->group( item->data(0, Qt::UserRole).toByteArray() ); // Group name = debug area code = cb's name
140 if (item->checkState(0) != Qt::PartiallyChecked)
141 {
142 int setting = (item->checkState(0) == Qt::Checked) ? 2 : 4;
143 group.writeEntry( "InfoOutput", setting );
144 }
145 }
146 //sync done by main.cpp
147
148 // send DBus message to all clients
149 QDBusMessage msg = QDBusMessage::createSignal("/", "org.kde.KDebug", "configChanged" );
150 if (!QDBusConnection::sessionBus().send(msg))
151 {
152 kError() << "Unable to send D-BUS message" << endl;
153 }
154}
155
156void KListDebugDialog::activateArea( const QByteArray& area, bool activate )
157{
158 foreach(QTreeWidgetItem* item, m_areaWidget->findItems(area, Qt::MatchContains)) {
159 item->setCheckState( 0, activate ? Qt::Checked : Qt::Unchecked );
160 return;
161 }
162}
163
164void KListDebugDialog::disableAllClicked()
165{
166 bool allDisabled = m_disableAll->isChecked();
167 m_incrSearch->setEnabled(!allDisabled);
168 m_areaWidget->setEnabled(!allDisabled);
169 m_buttonContainer->setEnabled(!allDisabled);
170}
171
172#include "klistdebugdialog.moc"
173