OSDN Git Service

831e43587b7b9f6e687475fcf6274aa318aff9c4
[kita/kita.git] / kita / src / threadtabwidget.cpp
1 /***************************************************************************
2 *   Copyright (C) 2004 by Kita Developers                                 *
3 *   ikemo@users.sourceforge.jp                                            *
4 *                                                                         *
5 *   This program is free software; you can redistribute it and/or modify  *
6 *   it under the terms of the GNU General Public License as published by  *
7 *   the Free Software Foundation; either version 2 of the License, or     *
8 *   (at your option) any later version.                                   *
9 ***************************************************************************/
10
11 #include "threadtabwidget.h"
12
13 #include <QtGui/QApplication>
14 #include <QtGui/QClipboard>
15 #include <QtGui/QContextMenuEvent>
16
17 #include <kaction.h>
18 #include <kactioncollection.h>
19 #include <klocale.h>
20 #include <kmenu.h>
21 #include <kstandardaction.h>
22 #include <kstandardshortcut.h>
23
24 #include "threadview.h"
25 #include "viewmediator.h"
26 #include "libkita/datmanager.h"
27 #include "libkita/globalconfig.h"
28 #include "libkita/kita_misc.h"
29
30 using namespace Kita;
31
32 ThreadTabWidget::ThreadTabWidget(QWidget* parent) : TabWidgetBase(parent)
33 {
34     setXMLFile("threadtabwidgetui.rc");
35
36     m_actionList = actionCollection()->actions();
37
38     setupActions();
39
40     /* default view */
41     createView(i18nc("@title:tab", "Thread"));
42 }
43
44
45 ThreadTabWidget::~ThreadTabWidget() {}
46
47 /* show "Main thread" view */  /* public slots */
48 void ThreadTabWidget::slotShowMainThread(const KUrl& url)
49 {
50     QString refstr;
51     KUrl datUrl = getDatUrl(url, refstr);
52     QString threadName = DatManager(datUrl).threadName();
53     int jumpNum = 0;
54
55     int viewMode = VIEWMODE_MAINVIEW;
56     ThreadView* currentView = isThreadView(currentWidget());
57     if (currentView) viewMode = currentView->getViewMode();
58
59     if (!refstr.isEmpty()) {
60         int i = refstr.indexOf("-");
61         if (i != -1) jumpNum = refstr.left(i).toInt();
62         else jumpNum = refstr.toInt();
63     }
64
65     ThreadView* view = findMainView(datUrl);
66
67     if (view) {
68
69         setCurrentWidget(view);
70
71         if (view->threadUrl().isEmpty()) {
72             /* Show on the default view */
73             view->showThread(datUrl, jumpNum);
74         } else {
75             /* The view is already shown */
76             /* TODO: jump to jumpNum after reloading */
77
78             view->slotReloadButton(jumpNum);
79         }
80
81     } else {
82         ThreadView * newView = createView(threadName);
83
84         if (newView) {
85             newView->showThread(datUrl, jumpNum);
86             setCurrentWidget(newView);
87         }
88     }
89
90     slotUpdateThreadTab(datUrl);
91 }
92
93 /* close "all" views which URL is url. */ /* public slot */
94 void ThreadTabWidget::slotCloseThreadTab(const KUrl& url)
95 {
96     int max = count();
97     if (max == 0) return ;
98     KUrl datUrl = getDatUrl(url);
99
100     int i, i2;
101     i = i2 = 0;
102     while (i < max) {
103         ThreadView * view = isThreadView(widget(i));
104         if (view) {
105             if (view->datUrl() == datUrl) {
106                 slotCloseTab(i2);
107                 i2--;
108             }
109         }
110         i++; i2++;
111     }
112 }
113
114
115
116 /* create ThreadView */   /* private */
117 ThreadView* ThreadTabWidget::createView(const QString& label)
118 {
119     ThreadView * view = new ThreadView(this);
120     if (view) {
121         addTab(view, label);
122     }
123
124     return view;
125 }
126
127 /* private */
128 ThreadView* ThreadTabWidget::findMainView(const KUrl& url)
129 {
130     KUrl datUrl = getDatUrl(url);
131
132     int max = count();
133     if (max == 0) return 0;
134     int i = 0;
135
136     while (i < max) {
137         ThreadView * view = isThreadView(widget(i));
138         if (view) {
139             if (view->getViewMode() == VIEWMODE_MAINVIEW) {
140
141                 if (view->datUrl() == datUrl
142                         || view->datUrl().isEmpty()  /* default view */
143                   ) return view;
144             }
145         }
146         i++;
147     }
148
149     return 0;
150 }
151
152
153 /* private */
154 ThreadView* ThreadTabWidget::isThreadView(QWidget* w)
155 {
156     return qobject_cast<ThreadView*>(w);
157 }
158
159
160 /* private slots */
161 void ThreadTabWidget::slotUpdateThreadTab(const KUrl& url)
162 {
163     KUrl datUrl = getDatUrl(url);
164
165     ThreadView * view = findMainView(datUrl);
166     if (view) {
167         QString threadName = DatManager(datUrl).threadName();
168
169         setTabText(indexOf(view), threadName);
170         setTabToolTip(indexOf(view), threadName);
171     }
172 }
173
174 void ThreadTabWidget::slotFontChanged()
175 {
176     QFont font = GlobalConfig::threadFont();
177     setFont(font);
178 }
179
180 /* protected */ /* virtual */
181 void ThreadTabWidget::deleteWidget(QWidget* w)
182 {
183     TabWidgetBase::deleteWidget(w);
184
185     if (count() == 0) {
186         ViewMediator::getInstance()->setMainCaption(QString());
187         ViewMediator::getInstance()->setMainStatus(QString());
188         ViewMediator::getInstance()->setMainUrlLine(KUrl());
189
190         /* default view */
191         ThreadView * threadView = createView(i18nc("@title:tab", "Thread"));
192
193         if (threadView) {
194             setCurrentWidget(threadView);
195         }
196     }
197 }
198
199
200
201 /*--------------------------------*/
202 /* ThreadView actions */
203
204
205 /* private */
206 void ThreadTabWidget::setupActions()
207 {
208     KStandardAction::copy(this, SLOT(slotCopyText()),
209             actionCollection());
210     KStandardAction::find(this, SLOT(slotFocusSearchCombo()),
211             actionCollection());
212     KStandardAction::findNext(this, SLOT(slotSearchNext()),
213             actionCollection());
214     KStandardAction::findPrev(this, SLOT(slotSearchPrev()),
215             actionCollection());
216
217     KAction* gobackAction = actionCollection()->addAction("threadview_goback");
218     gobackAction->setText(i18nc("@action:inmenu", "Go Back Anchor"));
219     gobackAction->setShortcut(Qt::Key_Backspace);
220     gobackAction->setIcon(SmallIcon("arrow-left-double"));
221     connect(gobackAction, SIGNAL(triggered()), this, SLOT(slotGobackAnchor()));
222
223     KAction* gotoheaderAction
224         = actionCollection()->addAction("threadview_gotoheader");
225     gotoheaderAction->setText(i18nc("@action:inmenu", "Start"));
226     gotoheaderAction->setShortcut(Qt::Key_Home);
227     gotoheaderAction->setIcon(SmallIcon("go-up"));
228     connect(gotoheaderAction, SIGNAL(triggered()),
229             this, SLOT(slotGotoHeader()));
230
231     KAction* gotofooterAction
232         = actionCollection()->addAction("threadview_gotofooter");
233     gotofooterAction->setText(i18nc("@action:inmenu", "End"));
234     gotofooterAction->setShortcut(Qt::Key_End);
235     gotofooterAction->setIcon(SmallIcon("go-down"));
236     connect(gotofooterAction, SIGNAL(triggered()), this, SLOT(slotGotoFooter()));
237
238     KAction* reload_action
239         = actionCollection()->addAction("threadview_reload");
240     reload_action->setText(i18nc("@action:button", "Reload"));
241     reload_action->setShortcut(KStandardShortcut::reload());
242     reload_action->setIcon(SmallIcon("view-refresh"));
243     connect(reload_action, SIGNAL(triggered()), this, SLOT(slotReloadButton()));
244
245     KAction* stopAction = actionCollection()->addAction("threadview_stop");
246     stopAction->setText(i18nc("@action:inmenu", "Stop"));
247     stopAction->setShortcut(Qt::Key_Escape);
248     stopAction->setIcon(SmallIcon("process-stop"));
249     connect(stopAction, SIGNAL(triggered()), this, SLOT(slotStopLoading()));
250
251     KAction* deleteAction = actionCollection()->addAction("threadview_delete");
252     deleteAction->setText(i18nc("@action:button", "Delete"));
253     deleteAction->setShortcut(Qt::Key_Delete);
254     deleteAction->setIcon(SmallIcon("trash-empty"));
255     connect(deleteAction, SIGNAL(triggered()), this, SLOT(slotDeleteButtonClicked()));
256 }
257
258
259
260 /* ThreadView actions */
261
262 /* copy selected text (Ctrl+C) */ /* public slot */
263 void ThreadTabWidget::slotCopyText()
264 {
265     QWidget * w = currentWidget();
266     ThreadView * view = isThreadView(w);
267     if (view) {
268         QClipboard * clipboard = QApplication::clipboard();
269         QString text = view->selectedText();
270         clipboard->setText(text, QClipboard::Clipboard);
271     }
272 }
273
274 /* public slot  */
275 void ThreadTabWidget::slotFocusSearchCombo()
276 {
277     ThreadView * view = isThreadView(currentWidget());
278     if (view) view->focusSearchCombo();
279 }
280
281
282 /* public slot  */
283 void ThreadTabWidget::slotSearchNext()
284 {
285     ThreadView * view = isThreadView(currentWidget());
286     if (view) view->slotSearchNext();
287 }
288
289
290 /* public slot  */
291 void ThreadTabWidget::slotSearchPrev()
292 {
293     ThreadView * view = isThreadView(currentWidget());
294     if (view) view->slotSearchPrev();
295 }
296
297
298 /* public slot  */
299 void ThreadTabWidget::slotGobackAnchor()
300 {
301     ThreadView * view = isThreadView(currentWidget());
302     if (view) view->slotGobackAnchor();
303 }
304
305
306 /* public slot  */
307 void ThreadTabWidget::slotGotoHeader()
308 {
309     ThreadView * view = isThreadView(currentWidget());
310     if (view) view->slotGotoHeader();
311 }
312
313
314 /* public slot  */
315 void ThreadTabWidget::slotGotoFooter()
316 {
317     ThreadView * view = isThreadView(currentWidget());
318     if (view) view->slotGotoFooter();
319 }
320
321
322 /* public slot  */
323 void ThreadTabWidget::slotReloadButton()
324 {
325     ThreadView * view = isThreadView(currentWidget());
326     if (view) view->slotReloadButton();
327 }
328
329
330 /* public slot  */
331 void ThreadTabWidget::slotStopLoading()
332 {
333     ThreadView * view = isThreadView(currentWidget());
334     if (view) view->slotStopLoading();
335 }
336
337
338 /* public slot  */
339 void ThreadTabWidget::slotDeleteButtonClicked()
340 {
341     ThreadView * view = isThreadView(currentWidget());
342     if (view) view->slotDeleteButtonClicked();
343 }
344
345 void ThreadTabWidget::contextMenuEvent(QContextMenuEvent* e)
346 {
347     KMenu popup;
348     QAction* act;
349     foreach (act, m_actionList) {
350          popup.addAction(act);
351     }
352     popup.exec(e->globalPos());
353 /*    KActionCollection * collection = client->actionCollection();
354     KAction* action;
355     action = new KAction(i18n("goback anchor"), SmallIcon("idea"), KShortcut(), m_threadPart, SLOT(gobackAnchor()), collection, "goback_anchor");
356     emit popupMenu(client, global, url, mimeType, mode);*/ // TODO
357 }