1 | |
2 | /* |
3 | * infocenter.cpp |
4 | * |
5 | * Copyright (C) 2010 David Hubner <hubnerd@ntlworld.com> |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by |
9 | * the Free Software Foundation; either version 2 of the License, or |
10 | * (at your option) any later version. |
11 | * |
12 | * This program is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * GNU General Public License for more details. |
16 | * |
17 | * You should have received a copy of the GNU General Public License |
18 | * along with this program; if not, write to the Free Software |
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
20 | * |
21 | */ |
22 | |
23 | //Local |
24 | #include "infocenter.h" |
25 | |
26 | //KDE |
27 | #include <KLocale> |
28 | #include <KIcon> |
29 | #include <KMenuBar> |
30 | #include <KMenu> |
31 | #include <KHelpMenu> |
32 | #include <KConfig> |
33 | #include <KGlobal> |
34 | #include <KConfigGroup> |
35 | #include <KDebug> |
36 | #include <KToolInvocation> |
37 | #include <KActionMenu> |
38 | #include <kxmlguifactory.h> |
39 | #include <KStandardAction> |
40 | #include <KActionCollection> |
41 | #include <KAboutApplicationDialog> |
42 | #include <KMessageBox> |
43 | #include <KFileDialog> |
44 | #include <KShortcut> |
45 | #include <KToolBar> |
46 | |
47 | //QT |
48 | #include <QGridLayout> |
49 | #include <QProcess> |
50 | #include <QTextStream> |
51 | #include <QFile> |
52 | #include <QKeySequence> |
53 | |
54 | KInfoCenter::KInfoCenter() : KXmlGuiWindow( 0, Qt::WindowContextHelpButtonHint ) |
55 | { |
56 | setWindowIcon(KIcon("hwinfo" )); |
57 | setWindowTitle(i18nc("Main window title" , "KInfocenter" )); |
58 | setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
59 | |
60 | m_cWidget = new QWidget(this); |
61 | setCentralWidget(m_cWidget); |
62 | |
63 | QVBoxLayout *cLayout = new QVBoxLayout(m_cWidget); |
64 | Q_UNUSED(cLayout); |
65 | |
66 | cLayout->setSpacing(0); |
67 | cLayout->setContentsMargins(0, 0, 0, 0); |
68 | createMainFrame(); |
69 | createToolBar(); |
70 | |
71 | //TreeWidget |
72 | connect(m_sideMenu,SIGNAL(activated(const KcmTreeItem*)),this,SLOT(itemClickedSlot(const KcmTreeItem*))); |
73 | |
74 | //SearchBox |
75 | connect(m_searchText, SIGNAL(textChanged(QString)), m_sideMenu, SLOT(filterSideMenuSlot(QString))); |
76 | connect(m_searchAction, SIGNAL(triggered(Qt::MouseButtons,Qt::KeyboardModifiers)),m_searchText,SLOT(setFocus())); |
77 | |
78 | //Buttons |
79 | connect(m_moduleHelpAction, SIGNAL(triggered(bool)),this,SLOT(helpClickedSlot())); |
80 | connect(m_exportAction, SIGNAL(triggered(bool)),this,SLOT(exportClickedSlot())); |
81 | |
82 | //Menu |
83 | connect(m_aboutKcm, SIGNAL(triggered(bool)), this, SLOT(aboutKcmSlot())); |
84 | |
85 | //Startup |
86 | m_searchText->completionObject()->setItems(m_sideMenu->allChildKeywords()); |
87 | m_sideMenu->setFocus(Qt::OtherFocusReason); |
88 | m_sideMenu->changeToFirstValidItem(); |
89 | |
90 | m_toolTips = new ToolTipManager(m_sideMenu); |
91 | setupGUI(QSize(640,480), ToolBar | Keys | Save | Create,"kinfocenterui.rc" ); |
92 | |
93 | m_helpAction->setMenu( dynamic_cast<KMenu*>( factory()->container("help" , this) ) ); |
94 | menuBar()->hide(); |
95 | |
96 | QAction *aboutApp = actionCollection()->action("help_about_app" ); |
97 | aboutApp->setIcon(KIcon("hwinfo" )); |
98 | } |
99 | |
100 | KInfoCenter::~KInfoCenter() |
101 | { |
102 | delete m_toolTips; |
103 | |
104 | //TreeWidget |
105 | disconnect(m_sideMenu,SIGNAL(activated(const KcmTreeItem*)),this,SLOT(itemClickedSlot(const KcmTreeItem*))); |
106 | |
107 | //SearchBox |
108 | disconnect(m_searchText, SIGNAL(textChanged(QString)), m_sideMenu, SLOT(filterSideMenuSlot(QString))); |
109 | disconnect(m_searchAction, SIGNAL(triggered(Qt::MouseButtons,Qt::KeyboardModifiers)),m_searchText, SLOT(setFocus())); |
110 | |
111 | //Buttons |
112 | disconnect(m_moduleHelpAction, SIGNAL(triggered(bool)),this,SLOT(helpClickedSlot())); |
113 | disconnect(m_exportAction, SIGNAL(triggered(bool)),this,SLOT(exportClickedSlot())); |
114 | |
115 | //Menu |
116 | disconnect(m_aboutKcm, SIGNAL(triggered(bool)), this, SLOT(aboutKcmSlot())); |
117 | } |
118 | |
119 | bool KInfoCenter::eventFilter(QObject *watched, QEvent *event) |
120 | { |
121 | if (watched == m_sideMenu && event->type() == QEvent::Move) |
122 | { |
123 | m_contain->setKcmTopEdge(m_sideMenu->y()); |
124 | } |
125 | return false; |
126 | } |
127 | |
128 | void KInfoCenter::createToolBar() |
129 | { |
130 | KStandardAction::quit(this, SLOT(close()), actionCollection()); |
131 | KStandardAction::keyBindings(guiFactory(), SLOT(configureShortcuts()), actionCollection()); |
132 | |
133 | toolBar()->setMovable(false); |
134 | |
135 | m_aboutKcm = actionCollection()->addAction("help_about_module" ); |
136 | m_aboutKcm->setText(i18nc("Information about current module located in about menu" ,"About Current Information Module" )); |
137 | m_aboutKcm->setIcon(KIcon("help-about" )); |
138 | m_aboutKcm->setEnabled(false); |
139 | |
140 | m_exportAction = new KAction(this); |
141 | m_exportAction->setText(i18nc("Export button label" , "Export" )); |
142 | m_exportAction->setIcon(KIcon("document-export" )); |
143 | |
144 | m_moduleHelpAction = new KAction(this); |
145 | m_moduleHelpAction->setText(i18nc("Module help button label" , "Module Help" )); |
146 | m_moduleHelpAction->setIcon(KIcon("help-contextual" )); |
147 | |
148 | m_helpAction = new KActionMenu( KIcon("help-contents" ), i18nc("Help button label" ,"Help" ), this ); |
149 | m_helpAction->setDelayed( false ); |
150 | |
151 | actionCollection()->addAction("export" , m_exportAction); |
152 | actionCollection()->addAction("helpModule" , m_moduleHelpAction); |
153 | actionCollection()->addAction("helpMenu" , m_helpAction); |
154 | } |
155 | |
156 | void KInfoCenter::createMainFrame() |
157 | { |
158 | QWidget *mainDisplay = new QWidget(); |
159 | mainDisplay->setContentsMargins(0,0,0,0); |
160 | |
161 | QHBoxLayout *mainLayout = new QHBoxLayout(mainDisplay); |
162 | |
163 | m_splitter = new QSplitter(m_cWidget); |
164 | m_splitter->setContentsMargins(0, 0, 0, 0); |
165 | mainLayout->addWidget(m_splitter); |
166 | |
167 | createMenuFrame(); |
168 | |
169 | m_contain = new KcmContainer(m_splitter); |
170 | m_splitter->addWidget(m_contain); |
171 | |
172 | m_splitter->setStretchFactor(0, 0); |
173 | m_splitter->setStretchFactor(1, 1); |
174 | |
175 | m_cWidget->layout()->addWidget(mainDisplay); |
176 | } |
177 | |
178 | void KInfoCenter::() |
179 | { |
180 | QWidget *sideFrame = new QWidget(m_splitter); |
181 | sideFrame->setContentsMargins(0,0,0,0); |
182 | |
183 | QVBoxLayout * = new QVBoxLayout(sideFrame); |
184 | menuLayout->setContentsMargins(0, 0, 0, 0); |
185 | |
186 | m_searchText = new KLineEdit(sideFrame); |
187 | m_searchText->setClearButtonShown(true); |
188 | m_searchText->setClickMessage( i18nc( "Search Bar Click Message" , "Search" ) ); |
189 | m_searchText->setCompletionMode( KGlobalSettings::CompletionPopup ); |
190 | m_searchText->completionObject()->setIgnoreCase(true); |
191 | |
192 | m_searchAction = new KAction(this); |
193 | m_searchAction->setShortcut(KShortcut(QKeySequence(Qt::CTRL + Qt::Key_F))); |
194 | m_searchAction->setText(i18nc("Kaction search label" , "Search Modules" )); |
195 | m_searchAction->setIcon(KIcon("edit-find" )); |
196 | |
197 | actionCollection()->addAction("search" ,m_searchAction); |
198 | |
199 | m_sideMenu = new SidePanel(sideFrame); |
200 | m_sideMenu->installEventFilter(this); |
201 | |
202 | menuLayout->addWidget(m_searchText); |
203 | menuLayout->addWidget(m_sideMenu); |
204 | m_splitter->addWidget(sideFrame); |
205 | } |
206 | |
207 | void KInfoCenter::itemClickedSlot(const KcmTreeItem *item) |
208 | { |
209 | resetCondition(); |
210 | if(item->type() != KcmTreeItem::CATEGORY) setKcm(item); |
211 | } |
212 | |
213 | void KInfoCenter::setKcm(const KcmTreeItem *kcmItem) |
214 | { |
215 | m_contain->setKcm(kcmItem->kcm()); |
216 | |
217 | setButtons(m_contain->buttons()); |
218 | m_aboutKcm->setEnabled(true); |
219 | } |
220 | |
221 | void KInfoCenter::setButtons(const KCModule::Buttons buttons) |
222 | { |
223 | if (buttons & KCModule::Help) m_moduleHelpAction->setEnabled(true); |
224 | if (buttons & KCModule::Export) m_exportAction->setEnabled(true); |
225 | } |
226 | |
227 | void KInfoCenter::resetCondition() |
228 | { |
229 | m_moduleHelpAction->setEnabled(false); |
230 | m_exportAction->setEnabled(false); |
231 | |
232 | m_aboutKcm->setEnabled(false); |
233 | } |
234 | |
235 | void KInfoCenter::helpClickedSlot() |
236 | { |
237 | // Nicked from Ben, could not use KToolInvocation due to docpath. |
238 | QString docPath = m_contain->helpPath(); |
239 | |
240 | KUrl url( KUrl("help:/" ), docPath ); |
241 | QProcess::startDetached("khelpcenter" , QStringList() << url.url()); |
242 | } |
243 | |
244 | void KInfoCenter::exportClickedSlot() |
245 | { |
246 | QString moduleName = m_contain->name(); |
247 | |
248 | if(m_contain->exportText().isEmpty()) |
249 | { |
250 | KInfoCenter::showError(this,i18n("Export of the module has produced no output." )); |
251 | return; |
252 | } |
253 | |
254 | QString fileName = KFileDialog::getSaveFileName(KUrl(moduleName + ".txt" ),QString(),this); |
255 | if(fileName.isEmpty()) return; |
256 | |
257 | QFile exportFile(fileName); |
258 | |
259 | if(!exportFile.open(QIODevice::WriteOnly)) |
260 | { |
261 | KInfoCenter::showError(this,i18n("Unable to open file to write export information" )); |
262 | return; |
263 | } |
264 | |
265 | QTextStream exportTextStream( &exportFile ); |
266 | exportTextStream << (i18n("Export information for %1" , moduleName)) |
267 | << "\n\n" << m_contain->exportText() << endl; |
268 | |
269 | exportFile.close(); |
270 | KInfoCenter::showError(this, i18n("Information exported" )); |
271 | } |
272 | |
273 | void KInfoCenter::aboutKcmSlot() |
274 | { |
275 | KAboutApplicationDialog kcmAboutDialog(m_contain->kcmAboutData()); |
276 | kcmAboutDialog.exec(); |
277 | } |
278 | |
279 | void KInfoCenter::showError(QWidget *parent, const QString& errorMessage) |
280 | { |
281 | KMessageBox::sorry(parent, errorMessage); |
282 | } |
283 | |