OSDN Git Service

9ba0940d78965c96d2c8fab95e315cd566da7371
[kita/kita.git] / kita / src / boardtabwidget.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 "boardtabwidget.h"
12
13 #include <QtGui/QApplication>
14 #include <QtGui/QClipboard>
15
16 #include <kaction.h>
17 #include <kactioncollection.h>
18 #include <kmenu.h>
19 #include <krun.h>
20 #include <kstandardshortcut.h>
21
22 #include "boardview.h"
23 #include "favoritelistview.h"
24 #include "viewmediator.h"
25 #include "libkita/boarddatabase.h"
26
27 using namespace Kita;
28
29 BoardTabWidget::BoardTabWidget(QWidget* parent) : TabWidgetBase(parent)
30 {
31     setXMLFile("boardtabwidgetui.rc");
32
33     setTabBar(new SubjectTabBar(this));
34
35     FavoriteListView* favoriteList = new FavoriteListView(this);
36     favoriteList->setObjectName("favoriteTab");
37     ViewMediator::getInstance()->setFavoriteListView(favoriteList);
38     addTab(favoriteList, i18nc("@title:tab Favorite boards", "Favorite"));
39
40     setupActions();
41 }
42
43 BoardTabWidget::~BoardTabWidget()
44 {}
45
46 void BoardTabWidget::updateBoardView(const KUrl& datUrl)
47 {
48     for(int i=0; i<count(); i++) {
49         BoardView* view = isSubjectView(widget(i));
50         if(view) {
51             view->slotUpdateSubject(datUrl);
52         }
53     }
54 }
55
56 /* public slots */
57 void BoardTabWidget::loadBoard(const KUrl& boardUrl)
58 {
59     BoardView * view = findView(boardUrl);
60     BoardDatabase db(boardUrl);
61     QString boardName = db.boardName();
62     if (!view) {
63         view = createView(boardName);
64     }
65
66     if (view) {
67         setCurrentWidget(view);
68         view->loadBoard(boardUrl);
69     }
70 }
71
72 /* create BoardView */ /* private */
73 BoardView* BoardTabWidget::createView(QString label)
74 {
75     BoardView * view = new BoardView(this);
76     if (view) {
77         insertTab(count() - 1, view, label);
78     }
79
80     return view;
81 }
82
83 /* private */
84 BoardView* BoardTabWidget::findView(const KUrl& boardUrl)
85 {
86     int max = count() - 1;
87     if (max <= 0) return 0;
88     int i = 0;
89
90     while (i < max) {
91         BoardView * view = isSubjectView(widget(i));
92         if (view && view->boardUrl() == boardUrl) return view;
93         i++;
94     }
95
96     return 0;
97 }
98
99
100 /* protected */ /* virtual */
101 void BoardTabWidget::deleteWidget(QWidget* w)
102 {
103     BoardView * view = isSubjectView(w);
104     if (!view) return ;
105     removePage(view);
106     delete view;
107 }
108
109
110 /* private */
111 BoardView* BoardTabWidget::isSubjectView(QWidget* w)
112 {
113     return qobject_cast<BoardView*>(w);
114 }
115
116 /*--------------------------------*/
117 /* BoardView actions */
118
119 /* private */
120 void BoardTabWidget::setupActions()
121 {
122     KAction* find_action = actionCollection()->addAction("subjectview_find");
123     find_action->setText(i18n("Find"));
124     find_action->setShortcut(KStandardShortcut::find());
125     connect(find_action, SIGNAL(triggered()), this, SLOT(slotFocusSearchCombo()));
126
127     KAction* reload_action = actionCollection()->addAction("subjectview_reload");
128     reload_action->setText(i18nc("@action:button Reload the subject view",
129             "Reload"));
130     reload_action->setShortcut(KStandardShortcut::reload());
131     connect(reload_action, SIGNAL(triggered()), this, SLOT(slotReloadButton()));
132
133     KAction* showoldlogs_action = actionCollection()->addAction("subjectview_showoldlogs");
134     showoldlogs_action->setText(i18n("Show Old Logs"));
135     connect(showoldlogs_action, SIGNAL(triggered()), this, SLOT(slotShowOldLogs()));
136 }
137
138 /* public slot */
139 void BoardTabWidget::slotReloadButton()
140 {
141     BoardView * view = isSubjectView(currentWidget());
142     if (view) view->reloadSubject();
143 }
144
145 /* public slot */
146 void BoardTabWidget::slotFocusSearchCombo()
147 {
148     BoardView * view = isSubjectView(currentWidget());
149     if (view) view->slotFocusSearchCombo();
150 }
151
152 /* public slot */
153 void BoardTabWidget::slotShowOldLogs(int idx)
154 {
155     BoardView * view;
156     if (idx == -1) view = isSubjectView(currentWidget());
157     else view = isSubjectView(widget(idx));
158     if (view) view->toggleShowOldLogs();
159 }
160
161 /*---------------------------------------------------------------------*/
162 /*---------------------------------------------------------------------*/
163 /*---------------------------------------------------------------------*/
164
165
166
167 SubjectTabBar::SubjectTabBar(QWidget* parent)
168         : KTabBar(parent)
169 {
170     connect(this, SIGNAL(contextMenu(int, const QPoint&)),
171              SLOT(showPopupMenu(int, const QPoint&)));
172     m_closeAct = new KAction(i18nc("@action:inmenu", "Close This Tab"), this);
173     m_closeOtherAct
174         = new KAction(i18nc("@action:inmenu", "Close Other Tabs"), this);
175     m_closeRightAct
176         = new KAction(i18nc("@action:inmenu", "Close Right Tabs"), this);
177     m_closeLeftAct
178         = new KAction(i18nc("@action:inmenu", "Close Left Tabs"), this);
179     m_showOldLogsAct
180         = new KAction(i18nc("@action:inmenu", "Show Old Logs"), this);
181     m_openBrowserAct
182         = new KAction(i18nc("@action:inmenu", "Open with Web Browser"), this);
183     m_copyTitleAct
184         = new KAction(i18nc("@action:inmenu", "Copy Title and URL"), this);
185 }
186
187 SubjectTabBar::~SubjectTabBar()
188 {}
189
190
191 /* private */ /* virtual */
192 void SubjectTabBar::showPopupMenu(int idx, const QPoint& global)
193 {
194     BoardTabWidget* tabwidget = static_cast<BoardTabWidget*>(parentWidget());
195     KActionCollection * collection = tabwidget->actionCollection();
196
197     if (QString::compare(tabwidget->widget(idx)->objectName(), "favoriteTab")
198             == 0)
199         return;
200     KMenu popup(this);
201     popup.addAction(m_closeAct);
202     popup.addAction(collection->action("tab_prevtab"));
203     popup.addAction(collection->action("tab_nexttab"));
204     popup.addSeparator();
205
206     popup.addAction(m_closeOtherAct);
207     popup.addAction(m_closeRightAct);
208     popup.addAction(m_closeLeftAct);
209     popup.addAction(collection->action("tab_closealltab"));
210     popup.addSeparator();
211
212     popup.addAction(m_showOldLogsAct);
213     popup.addAction(m_openBrowserAct);
214     popup.addAction(m_copyTitleAct);
215     popup.addSeparator();
216
217     popup.addAction(collection->action("tab_configkeys"));
218
219     BoardView* subjectView = static_cast<BoardView *>(tabwidget->widget(idx));
220     QClipboard* clipboard = QApplication::clipboard();
221
222     QAction* action = popup.exec(global);
223     if (action == m_closeAct) {
224         tabwidget->slotCloseTab(idx);
225     } else if (action == m_closeOtherAct) {
226         tabwidget->slotCloseOtherTab(idx);
227     } else if (action == m_closeRightAct) {
228         tabwidget->slotCloseRightTab(idx);
229     } else if (action == m_closeLeftAct) {
230         tabwidget->slotCloseLeftTab(idx);
231     } else if (action == m_showOldLogsAct) {
232         tabwidget->slotShowOldLogs(idx);
233     } else if (action == m_openBrowserAct) {
234         KRun::runUrl(subjectView->boardUrl(), "text/html", this);
235     } else if (action == m_copyTitleAct) {
236         BoardDatabase db(subjectView->boardUrl());
237         QString cliptxt = db.boardName()
238             + '\n' + subjectView->boardUrl().prettyUrl();
239         clipboard->setText(cliptxt , QClipboard::Clipboard);
240         clipboard->setText(cliptxt , QClipboard::Selection);
241     }
242 }