File: infocenter.cpp

package info (click to toggle)
kde-workspace 4%3A4.8.4-6
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 89,920 kB
  • sloc: cpp: 373,584; ansic: 35,020; xml: 7,435; perl: 1,550; sh: 1,329; ruby: 1,135; python: 646; asm: 566; makefile: 38
file content (282 lines) | stat: -rw-r--r-- 8,734 bytes parent folder | download
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282

/*
 *  infocenter.cpp
 *
 *  Copyright (C) 2010 David Hubner <hubnerd@ntlworld.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

//Local
#include "infocenter.h"

//KDE
#include <KLocale>
#include <KIcon>
#include <KMenuBar>
#include <KMenu>
#include <KHelpMenu>
#include <KConfig>
#include <KGlobal>
#include <KConfigGroup>
#include <KDebug>
#include <KToolInvocation>
#include <KActionMenu>
#include <kxmlguifactory.h>
#include <KStandardAction>
#include <KActionCollection>
#include <KAboutApplicationDialog>
#include <KMessageBox>
#include <KFileDialog>
#include <KShortcut>
#include <KToolBar>

//QT
#include <QGridLayout>
#include <QProcess>
#include <QTextStream>
#include <QFile>
#include <QKeySequence>

KInfoCenter::KInfoCenter() : KXmlGuiWindow( 0, Qt::WindowContextHelpButtonHint )
{ 
  setWindowIcon(KIcon("hwinfo"));
  setWindowTitle(i18nc("Main window title", "KInfocenter"));
  setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  
  m_cWidget = new QWidget(this);
  setCentralWidget(m_cWidget);
  
  QVBoxLayout *cLayout = new QVBoxLayout(m_cWidget);
  Q_UNUSED(cLayout); 
  
  cLayout->setSpacing(0);
  cLayout->setContentsMargins(0, 0, 0, 0);
  createMainFrame();
  createToolBar();
  
  //TreeWidget
  connect(m_sideMenu,SIGNAL(activated(const KcmTreeItem*)),this,SLOT(itemClickedSlot(const KcmTreeItem*)));

  //SearchBox
  connect(m_searchText, SIGNAL(textChanged(QString)), m_sideMenu, SLOT(filterSideMenuSlot(QString)));
  connect(m_searchAction, SIGNAL(triggered(Qt::MouseButtons,Qt::KeyboardModifiers)),m_searchText,SLOT(setFocus()));
  
  //Buttons
  connect(m_moduleHelpAction, SIGNAL(triggered(bool)),this,SLOT(helpClickedSlot()));
  connect(m_exportAction, SIGNAL(triggered(bool)),this,SLOT(exportClickedSlot()));
  
  //Menu
  connect(m_aboutKcm, SIGNAL(triggered(bool)), this, SLOT(aboutKcmSlot()));
  
  //Startup 
  m_searchText->completionObject()->setItems(m_sideMenu->allChildKeywords());
  m_sideMenu->setFocus(Qt::OtherFocusReason);
  m_sideMenu->changeToFirstValidItem();
  
  m_toolTips = new ToolTipManager(m_sideMenu);
  setupGUI(QSize(640,480),ToolBar | Keys | Save | Create,"kinfocenterui.rc");
  
  m_helpAction->setMenu( dynamic_cast<KMenu*>( factory()->container("help", this) ) );
  menuBar()->hide();
  
  QAction *aboutApp = actionCollection()->action("help_about_app");
  aboutApp->setIcon(KIcon("hwinfo"));
}

KInfoCenter::~KInfoCenter()
{ 
  delete m_toolTips;
  
  //TreeWidget
  disconnect(m_sideMenu,SIGNAL(activated(const KcmTreeItem*)),this,SLOT(itemClickedSlot(const KcmTreeItem*)));
  
  //SearchBox
  disconnect(m_searchText, SIGNAL(textChanged(QString)), m_sideMenu, SLOT(filterSideMenuSlot(QString)));
  disconnect(m_searchAction, SIGNAL(triggered(Qt::MouseButtons,Qt::KeyboardModifiers)),m_searchText, SLOT(setFocus()));
  
  //Buttons
  disconnect(m_moduleHelpAction, SIGNAL(triggered(bool)),this,SLOT(helpClickedSlot()));
  disconnect(m_exportAction, SIGNAL(triggered(bool)),this,SLOT(exportClickedSlot()));
  
  //Menu
  disconnect(m_aboutKcm, SIGNAL(triggered(bool)), this, SLOT(aboutKcmSlot()));
}

bool KInfoCenter::eventFilter(QObject *watched, QEvent *event)
{
  if (watched == m_sideMenu && event->type() == QEvent::Move)
  {
    m_contain->setKcmTopEdge(m_sideMenu->y());
  }
  return false;
}

void KInfoCenter::createToolBar()
{
  KStandardAction::quit(this, SLOT(close()), actionCollection());
  KStandardAction::keyBindings(guiFactory(), SLOT(configureShortcuts()), actionCollection());
  
  toolBar()->setMovable(false);
  
  m_aboutKcm = actionCollection()->addAction("help_about_module");
  m_aboutKcm->setText(i18nc("Information about current module located in about menu","About Current Information Module"));
  m_aboutKcm->setIcon(KIcon("help"));
  m_aboutKcm->setEnabled(false);
  
  m_exportAction = new KAction(this);
  m_exportAction->setText(i18nc("Export button label","Export"));
  m_exportAction->setIcon(KIcon("document-export"));
  
  m_moduleHelpAction = new KAction(this);
  m_moduleHelpAction->setText(i18nc("Module help button label","Module Help"));
  m_moduleHelpAction->setIcon(KIcon("help-contextual"));
  
  m_helpAction = new KActionMenu( KIcon("help-contents"), i18nc("Help button label","Help"), this );
  m_helpAction->setDelayed( false );
  
  actionCollection()->addAction("export",m_exportAction);
  actionCollection()->addAction("helpModule",m_moduleHelpAction);
  actionCollection()->addAction("helpMenu",m_helpAction);
}

void KInfoCenter::createMainFrame()
{ 
  QWidget *mainDisplay = new QWidget();
  mainDisplay->setContentsMargins(0,0,0,0);

  QHBoxLayout *mainLayout = new QHBoxLayout(mainDisplay);
  
  m_splitter = new QSplitter(m_cWidget);
  m_splitter->setContentsMargins(0, 0, 0, 0);
  mainLayout->addWidget(m_splitter);
  
  createMenuFrame();
  
  m_contain = new KcmContainer(m_splitter);
  m_splitter->addWidget(m_contain);
  
  m_splitter->setStretchFactor(0, 0);
  m_splitter->setStretchFactor(1, 1);
  
  m_cWidget->layout()->addWidget(mainDisplay);
}
  
void KInfoCenter::createMenuFrame() 
{
  QWidget *sideFrame = new QWidget(m_splitter);
  sideFrame->setContentsMargins(0,0,0,0);
  
  QVBoxLayout *menuLayout = new QVBoxLayout(sideFrame);
  menuLayout->setContentsMargins(0, 0, 0, 0);

  m_searchText = new KLineEdit(sideFrame);
  m_searchText->setClearButtonShown(true);
  m_searchText->setClickMessage( i18nc( "Search Bar Click Message", "Search" ) );
  m_searchText->setCompletionMode( KGlobalSettings::CompletionPopup );
  m_searchText->completionObject()->setIgnoreCase(true);
  
  m_searchAction = new KAction(this);
  m_searchAction->setShortcut(KShortcut(QKeySequence(Qt::CTRL + Qt::Key_F)));
  m_searchAction->setText(i18nc("Kaction search label", "Search Modules"));
  m_searchAction->setIcon(KIcon("search"));
  
  actionCollection()->addAction("search",m_searchAction);
  
  m_sideMenu = new SidePanel(sideFrame);
  m_sideMenu->installEventFilter(this);
  
  menuLayout->addWidget(m_searchText);
  menuLayout->addWidget(m_sideMenu);
  m_splitter->addWidget(sideFrame);
}
  
void KInfoCenter::itemClickedSlot(const KcmTreeItem *item) 
{ 
  resetCondition();  
  if(item->type() != KcmTreeItem::CATEGORY) setKcm(item);
}

void KInfoCenter::setKcm(const KcmTreeItem *kcmItem) 
{ 
  m_contain->setKcm(kcmItem->kcm());
  
  setButtons(m_contain->buttons());
  m_aboutKcm->setEnabled(true);
}

void KInfoCenter::setButtons(const KCModule::Buttons buttons) 
{    
  if (buttons & KCModule::Help) m_moduleHelpAction->setEnabled(true);
  if (buttons & KCModule::Export) m_exportAction->setEnabled(true);
}

void KInfoCenter::resetCondition()
{
  m_moduleHelpAction->setEnabled(false);
  m_exportAction->setEnabled(false);

  m_aboutKcm->setEnabled(false);
}

void KInfoCenter::helpClickedSlot() 
{ 
  // Nicked from Ben, could not use KToolInvocation due to docpath.  
  QString docPath = m_contain->helpPath();
  
  KUrl url( KUrl("help:/"), docPath );
  QProcess::startDetached("khelpcenter", QStringList() << url.url());
}

void KInfoCenter::exportClickedSlot() 
{ 
  QString moduleName = m_contain->name();
  
  if(m_contain->exportText().isEmpty())
  {
    KInfoCenter::showError(this,i18n("Export of the module has produced no output."));
    return;
  }
  
  QString fileName = KFileDialog::getSaveFileName(KUrl(moduleName + ".txt"),QString(),this);
  if(fileName.isEmpty()) return;
  
  QFile exportFile(fileName);

  if(!exportFile.open(QIODevice::WriteOnly))
  {
    KInfoCenter::showError(this,i18n("Unable to open file to write export information"));
    return;
  }
  
  QTextStream exportTextStream( &exportFile );
  exportTextStream << (i18n("Export information for %1",moduleName))
  << "\n\n" << m_contain->exportText() << endl;
  
  exportFile.close();
  KInfoCenter::showError(this,i18n("Information exported"));
}

void KInfoCenter::aboutKcmSlot()
{
  KAboutApplicationDialog kcmAboutDialog(m_contain->kcmAboutData());
  kcmAboutDialog.exec();
}

void KInfoCenter::showError(QWidget *parent, const QString& errorMessage)
{
  KMessageBox::sorry(parent,errorMessage);
}