1 /*
2 * Kchmviewer - a CHM and EPUB file viewer with broad language support
3 * Copyright (C) 2004-2014 George Yunaev, gyunaev@ulduzsoft.com
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 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "mainwindow.h"
20 #include "viewwindow.h"
21 #include "version.h"
22 #include "config.h"
23 #include "tab_bookmarks.h"
24
25 class BookmarkItem : public QListWidgetItem
26 {
27 public:
BookmarkItem(TabBookmarks * widget,QListWidget * parent,const QString & name,const QString & url,int pos)28 BookmarkItem( TabBookmarks * widget, QListWidget* parent, const QString& name, const QString& url, int pos )
29 : QListWidgetItem( parent )
30 {
31 m_name = name;
32 m_url = url;
33 m_scroll_y = pos;
34 m_action = new QAction( name, widget );
35 m_action->setData( qVariantFromValue( (void*) this ) );
36
37 QObject::connect( m_action,
38 SIGNAL( triggered() ),
39 widget,
40 SLOT( actionBookmarkActivated() ) );
41 }
42
setName(const QString & name)43 void setName( const QString& name )
44 {
45 m_name = name;
46 }
47
48 // Visualization
data(int role) const49 virtual QVariant data ( int role ) const
50 {
51 switch ( role )
52 {
53 case Qt::ToolTipRole:
54 case Qt::WhatsThisRole:
55 case Qt::DisplayRole:
56 return m_name;
57 }
58
59 return QVariant();
60 }
61
62 QString m_name;
63 QString m_url;
64 int m_scroll_y;
65 QAction * m_action;
66 };
67
68
69
TabBookmarks(QWidget * parent)70 TabBookmarks::TabBookmarks( QWidget *parent )
71 : QWidget( parent ), Ui::TabBookmarks()
72 {
73 // UIC code
74 setupUi( this );
75
76 if ( pConfig->m_tabUseSingleClick )
77 {
78 connect( list,
79 SIGNAL( itemClicked(QListWidgetItem*)),
80 this,
81 SLOT( onItemActivated( QListWidgetItem*)) );
82 }
83 else
84 {
85 connect( list,
86 SIGNAL( itemActivated(QListWidgetItem*)),
87 this,
88 SLOT( onItemActivated( QListWidgetItem*)) );
89 }
90
91 connect( btnAdd,
92 SIGNAL( clicked () ),
93 this,
94 SLOT( onAddBookmarkPressed( ) ) );
95
96 connect( btnDel,
97 SIGNAL( clicked () ),
98 this,
99 SLOT( onDelBookmarkPressed( ) ) );
100
101 connect( btnEdit,
102 SIGNAL( clicked () ),
103 this,
104 SLOT( onEditBookmarkPressed( ) ) );
105
106 m_menuBookmarks = 0;
107 m_contextMenu = 0;
108 m_listChanged = false;
109
110 // Activate custom context menu, and connect it
111 list->setContextMenuPolicy( Qt::CustomContextMenu );
112
113 connect( list,
114 SIGNAL( customContextMenuRequested ( const QPoint & ) ),
115 this,
116 SLOT( onContextMenuRequested( const QPoint & ) ) );
117
118 focus();
119 }
120
onAddBookmarkPressed()121 void TabBookmarks::onAddBookmarkPressed( )
122 {
123 bool ok;
124 QString url = ::mainWindow->currentBrowser()->getOpenedPage().toString();
125 QString title = ::mainWindow->chmFile()->getTopicByUrl(url);
126 QString name = QInputDialog::getText(
127 this,
128 i18n( "%1 - add a bookmark") . arg(QCoreApplication::applicationName()),
129 i18n( "Enter the name for this bookmark:" ),
130 QLineEdit::Normal,
131 title,
132 &ok );
133
134 if ( !ok || name.isEmpty() )
135 return;
136
137 BookmarkItem * item = new BookmarkItem ( this,
138 list,
139 name,
140 url,
141 ::mainWindow->currentBrowser()->getScrollbarPosition() );
142
143 m_menuBookmarks->addAction( item->m_action );
144 m_listChanged = true;
145 }
146
147
onDelBookmarkPressed()148 void TabBookmarks::onDelBookmarkPressed( )
149 {
150 BookmarkItem * item = (BookmarkItem *) list->currentItem();
151
152 if ( item )
153 {
154 m_menuBookmarks->removeAction( item->m_action );
155 delete item;
156 m_listChanged = true;
157 }
158 }
159
160
onEditBookmarkPressed()161 void TabBookmarks::onEditBookmarkPressed( )
162 {
163 BookmarkItem * item = (BookmarkItem *) list->currentItem();
164
165 if ( item )
166 {
167 bool ok;
168 QString name = QInputDialog::getText(
169 this,
170 i18n( "%1 - edit the bookmark name") . arg(QCoreApplication::applicationName()),
171 i18n( "Enter the name for this bookmark:" ),
172 QLineEdit::Normal,
173 item->m_name,
174 &ok );
175
176 if ( !ok || name.isEmpty() )
177 return;
178
179 item->setName( name );
180 item->m_action->setText( name );
181 m_listChanged = true;
182 update();
183 }
184 }
185
186
restoreSettings(const Settings::bookmark_saved_settings_t & settings)187 void TabBookmarks::restoreSettings( const Settings::bookmark_saved_settings_t & settings )
188 {
189 for ( int i = 0; i < settings.size(); i++ )
190 {
191 BookmarkItem * item = new BookmarkItem( this, list, settings[i].name, settings[i].url, settings[i].scroll_y );
192 m_menuBookmarks->addAction( item->m_action );
193 }
194 }
195
196
saveSettings(Settings::bookmark_saved_settings_t & settings)197 void TabBookmarks::saveSettings( Settings::bookmark_saved_settings_t & settings )
198 {
199 settings.clear();
200
201 for ( int i = 0; i < list->count(); i++ )
202 {
203 BookmarkItem * treeitem = (BookmarkItem *) list->item( i );
204 settings.push_back( Settings::SavedBookmark( treeitem->m_name, treeitem->m_url, treeitem->m_scroll_y) );
205 }
206 }
207
invalidate()208 void TabBookmarks::invalidate( )
209 {
210 for ( int i = 0; i < list->count(); i++ )
211 m_menuBookmarks->removeAction( ((BookmarkItem *) list->item( i ))->m_action );
212
213 list->clear();
214 }
215
focus()216 void TabBookmarks::focus()
217 {
218 if ( list->hasFocus() )
219 list->setFocus();
220 }
221
createMenu(QMenu * menuBookmarks)222 void TabBookmarks::createMenu( QMenu * menuBookmarks )
223 {
224 m_menuBookmarks = menuBookmarks;
225 }
226
onItemActivated(QListWidgetItem * item)227 void TabBookmarks::onItemActivated(QListWidgetItem * item)
228 {
229 if ( !item )
230 return;
231
232 BookmarkItem * treeitem = (BookmarkItem *) item;
233
234 if ( ::mainWindow->currentBrowser()->getOpenedPage().toString() != treeitem->m_url )
235 {
236 ::mainWindow->openPage( treeitem->m_url, MainWindow::OPF_CONTENT_TREE );
237 ::mainWindow->currentBrowser()->setScrollbarPosition( treeitem->m_scroll_y );
238 }
239 else
240 {
241 // Force the scroll since the page is not reloaded
242 ::mainWindow->currentBrowser()->setScrollbarPosition( treeitem->m_scroll_y, true );
243 }
244 }
245
actionBookmarkActivated()246 void TabBookmarks::actionBookmarkActivated()
247 {
248 QAction *action = qobject_cast< QAction * >(sender());
249
250 BookmarkItem * item = (BookmarkItem *) action->data().value< void* > ();
251 onItemActivated( item );
252 }
253
onContextMenuRequested(const QPoint & point)254 void TabBookmarks::onContextMenuRequested(const QPoint & point)
255 {
256 BookmarkItem * item = (BookmarkItem *) list->itemAt( point );
257
258 if( item )
259 {
260 ::mainWindow->currentBrowser()->setTabKeeper( item->m_url );
261 ::mainWindow->tabItemsContextMenu()->popup( list->viewport()->mapToGlobal( point ) );
262 }
263 }
264