Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / apps / keditbookmarks / bookmarklistview.cpp
blob9eceb5bfab6bd71891635b2e587e77c442affe5a
1 /* This file is part of the KDE project
2 Copyright (C) 2000 David Faure <faure@kde.org>
3 Copyright (C) 2002-2003 Alexander Kellett <lypanov@kde.org>
4 Copyright (C) 2005 Daniel Teske <teske@squorn.de>
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of
9 the License, or (at your option) version 3.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>
20 #include "bookmarklistview.h"
21 #include "bookmarkmodel.h"
22 #include "toplevel.h"
23 #include "settings.h"
24 #include "commands.h"
25 #include "treeitem_p.h"
26 #include <QtGui/QHeaderView>
27 #include <QtGui/QItemSelection>
28 #include <QtGui/QMenu>
29 #include <QtGui/QKeyEvent>
30 #include <QtGui/QBrush>
31 #include <QtGui/QPalette>
33 #include <kdebug.h>
35 BookmarkView::BookmarkView( QWidget * parent )
36 :QTreeView( parent )
38 setAcceptDrops(true);
41 BookmarkView::~BookmarkView()
46 /* ----------- */
48 BookmarkFolderView::BookmarkFolderView( BookmarkListView * view, QWidget * parent )
49 :BookmarkView( parent ), mview(view)
51 mmodel = new BookmarkFolderViewFilterModel(parent);
52 mmodel->setSourceModel(view->model());
53 setModel(mmodel);
54 header()->setVisible(false);
55 setRootIsDecorated(false);
56 expandAll();
57 setCurrentIndex( mmodel->index(0,0, QModelIndex()));
59 connect(mmodel, SIGNAL(modelReset()), this, SLOT(slotReset()));
63 BookmarkFolderView::~BookmarkFolderView()
68 void BookmarkFolderView::selectionChanged ( const QItemSelection & deselected, const QItemSelection & selected)
70 const QModelIndexList & list = selectionModel()->selectedIndexes();
71 if(list.count())
72 mview->setRootIndex( mmodel->mapToSource(list.at(0)) );
73 else
74 mview->setRootIndex( QModelIndex());
75 BookmarkView::selectionChanged( deselected, selected);
78 void BookmarkFolderView::slotReset()
80 setCurrentIndex( mmodel->index(0,0, QModelIndex()));
81 expandAll();
84 KBookmark BookmarkFolderView::bookmarkForIndex(const QModelIndex & idx) const
86 qDebug()<<"BookmarkFolderView::bookmarkForIndex"<<idx;
87 const QModelIndex & index = mmodel->mapToSource(idx);
88 return static_cast<KBookmarkModel *>(mmodel->sourceModel())->bookmarkForIndex(index);
92 /********/
95 BookmarkListView::BookmarkListView( QWidget * parent )
96 :BookmarkView( parent )
98 setDragEnabled(true);
102 void BookmarkListView::setModel(QAbstractItemModel * model)
104 BookmarkView::setModel(model);
107 KBookmarkModel* BookmarkListView::bookmarkModel() const
109 return dynamic_cast<KBookmarkModel*>(QTreeView::model());
112 KBookmark BookmarkListView::bookmarkForIndex(const QModelIndex & idx) const
114 return bookmarkModel()->bookmarkForIndex(idx);
118 BookmarkListView::~BookmarkListView()
120 saveColumnSetting();
123 void BookmarkListView::contextMenuEvent ( QContextMenuEvent * e )
125 QModelIndex index = indexAt(e->pos());
126 KBookmark bk;
127 if(index.isValid())
128 bk = bookmarkForIndex(index);
130 QMenu* popup;
131 if( !index.isValid()
132 || (bk.address() == CurrentMgr::self()->root().address())
133 || (bk.isGroup())) //FIXME add empty folder padder
135 popup = KEBApp::self()->popupMenuFactory("popup_folder");
137 else
139 popup = KEBApp::self()->popupMenuFactory("popup_bookmark");
141 if (popup)
142 popup->popup(e->globalPos());
145 void BookmarkListView::loadColumnSetting()
147 header()->resizeSection(KEBApp::NameColumn, KEBSettings::name());
148 header()->resizeSection(KEBApp::UrlColumn, KEBSettings::uRL());
149 header()->resizeSection(KEBApp::CommentColumn, KEBSettings::comment());
150 header()->resizeSection(KEBApp::StatusColumn, KEBSettings::status());
153 void BookmarkListView::saveColumnSetting()
155 KEBSettings::setName( header()->sectionSize(KEBApp::NameColumn));
156 KEBSettings::setURL( header()->sectionSize(KEBApp::UrlColumn));
157 KEBSettings::setComment( header()->sectionSize(KEBApp::CommentColumn));
158 KEBSettings::setStatus( header()->sectionSize(KEBApp::StatusColumn));
159 KEBSettings::self()->writeConfig();
162 /************/
164 BookmarkFolderViewFilterModel::BookmarkFolderViewFilterModel(QObject * parent)
165 : QSortFilterProxyModel(parent)
169 QStringList BookmarkFolderViewFilterModel::mimeTypes() const
171 return sourceModel()->mimeTypes();
174 bool BookmarkFolderViewFilterModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
176 // FIXME Probably bug in QT, send bug report
177 kDebug()<<"BookmarkFolderViewFilterModel::dropMimeData"<<endl;
178 QModelIndex idx;
179 if(row == -1)
180 idx = parent;
181 else
182 idx = index(row, column, parent);
183 QModelIndex src = mapToSource(idx);
184 return sourceModel()->dropMimeData( data, action, -1, -1, src);
187 BookmarkFolderViewFilterModel::~BookmarkFolderViewFilterModel()
191 bool BookmarkFolderViewFilterModel::filterAcceptsColumn ( int source_column, const QModelIndex & source_parent ) const
193 Q_UNUSED(source_parent);
195 //Show name, hide everything else
196 return (source_column == 0);
199 bool BookmarkFolderViewFilterModel::filterAcceptsRow ( int source_row, const QModelIndex & source_parent ) const
201 QModelIndex index = sourceModel()->index(source_row, 0, source_parent);
202 return static_cast<TreeItem *>(index.internalPointer())->bookmark().isGroup();
205 #include "bookmarklistview.moc"