Bookmarks: Code cosmetics

Spacing, namespaces, indirection, unused code...

Change-Id: Id2b0dccf7be0fc3b78305e16447f97546c6234bf
Reviewed-by: Christian Stenger <christian.stenger@digia.com>
This commit is contained in:
hjk
2014-06-24 10:28:16 +02:00
parent 272176ca0b
commit 06112d13aa
4 changed files with 57 additions and 86 deletions

View File

@@ -66,16 +66,10 @@ namespace Bookmarks {
namespace Internal { namespace Internal {
BookmarkDelegate::BookmarkDelegate(QObject *parent) BookmarkDelegate::BookmarkDelegate(QObject *parent)
: QStyledItemDelegate(parent), m_normalPixmap(0), m_selectedPixmap(0) : QStyledItemDelegate(parent)
{ {
} }
BookmarkDelegate::~BookmarkDelegate()
{
delete m_normalPixmap;
delete m_selectedPixmap;
}
QSize BookmarkDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const QSize BookmarkDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{ {
QStyleOptionViewItemV4 opt = option; QStyleOptionViewItemV4 opt = option;
@@ -90,14 +84,13 @@ QSize BookmarkDelegate::sizeHint(const QStyleOptionViewItem &option, const QMode
void BookmarkDelegate::generateGradientPixmap(int width, int height, const QColor &color, bool selected) const void BookmarkDelegate::generateGradientPixmap(int width, int height, const QColor &color, bool selected) const
{ {
QColor c = color; QColor c = color;
c.setAlpha(0); c.setAlpha(0);
QPixmap *pixmap = new QPixmap(width+1, height); QPixmap pixmap(width+1, height);
pixmap->fill(c); pixmap.fill(c);
QPainter painter(pixmap); QPainter painter(&pixmap);
painter.setPen(Qt::NoPen); painter.setPen(Qt::NoPen);
QLinearGradient lg; QLinearGradient lg;
@@ -107,7 +100,7 @@ void BookmarkDelegate::generateGradientPixmap(int width, int height, const QColo
lg.setColorAt(0, c); lg.setColorAt(0, c);
lg.setColorAt(0.4, color); lg.setColorAt(0.4, color);
painter.setBrush(lg); painter.setBrush(lg);
painter.drawRect(0, 0, width+1, height); painter.drawRect(0, 0, width+1, height);
if (selected) if (selected)
@@ -154,7 +147,7 @@ void BookmarkDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
// TopLeft // TopLeft
QString topLeft = index.data(BookmarkManager::Filename ).toString(); QString topLeft = index.data(BookmarkManager::Filename).toString();
painter->drawText(6, 2 + opt.rect.top() + fm.ascent(), topLeft); painter->drawText(6, 2 + opt.rect.top() + fm.ascent(), topLeft);
QString topRight = index.data(BookmarkManager::LineNumber).toString(); QString topRight = index.data(BookmarkManager::LineNumber).toString();
@@ -162,7 +155,7 @@ void BookmarkDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
int fwidth = fm.width(topLeft); int fwidth = fm.width(topLeft);
if (fwidth + lwidth > opt.rect.width()) { if (fwidth + lwidth > opt.rect.width()) {
int left = opt.rect.right() - lwidth; int left = opt.rect.right() - lwidth;
painter->drawPixmap(left, opt.rect.top(), selected? *m_selectedPixmap : *m_normalPixmap); painter->drawPixmap(left, opt.rect.top(), selected ? m_selectedPixmap : m_normalPixmap);
} }
// topRight // topRight
painter->drawText(opt.rect.right() - fm.width(topRight) - 6 , 2 + opt.rect.top() + fm.ascent(), topRight); painter->drawText(opt.rect.right() - fm.width(topRight) - 6 , 2 + opt.rect.top() + fm.ascent(), topRight);
@@ -213,24 +206,31 @@ void BookmarkDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
painter->restore(); painter->restore();
} }
BookmarkView::BookmarkView(QWidget *parent) : BookmarkView::BookmarkView(BookmarkManager *manager) :
Utils::ListView(parent), m_bookmarkContext(new Core::IContext(this)),
m_bookmarkContext(new BookmarkContext(this)), m_manager(manager)
m_manager(0)
{ {
setWindowTitle(tr("Bookmarks")); setWindowTitle(tr("Bookmarks"));
connect(this, SIGNAL(clicked(QModelIndex)), m_bookmarkContext->setWidget(this);
this, SLOT(gotoBookmark(QModelIndex))); m_bookmarkContext->setContext(Core::Context(Constants::BOOKMARKS_CONTEXT));
connect(this, SIGNAL(activated(QModelIndex)),
this, SLOT(gotoBookmark(QModelIndex)));
ICore::addContextObject(m_bookmarkContext); ICore::addContextObject(m_bookmarkContext);
Utils::ListView::setModel(manager);
setItemDelegate(new BookmarkDelegate(this)); setItemDelegate(new BookmarkDelegate(this));
setFrameStyle(QFrame::NoFrame); setFrameStyle(QFrame::NoFrame);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setFocusPolicy(Qt::NoFocus); setFocusPolicy(Qt::NoFocus);
setSelectionModel(manager->selectionModel());
setSelectionMode(QAbstractItemView::SingleSelection);
setSelectionBehavior(QAbstractItemView::SelectRows);
connect(this, SIGNAL(clicked(QModelIndex)),
this, SLOT(gotoBookmark(QModelIndex)));
connect(this, SIGNAL(activated(QModelIndex)),
this, SLOT(gotoBookmark(QModelIndex)));
} }
BookmarkView::~BookmarkView() BookmarkView::~BookmarkView()
@@ -311,17 +311,6 @@ void BookmarkView::removeAll()
} }
} }
void BookmarkView::setModel(QAbstractItemModel *model)
{
BookmarkManager *manager = qobject_cast<BookmarkManager *>(model);
QTC_ASSERT(manager, return);
m_manager = manager;
Utils::ListView::setModel(model);
setSelectionModel(manager->selectionModel());
setSelectionMode(QAbstractItemView::SingleSelection);
setSelectionBehavior(QAbstractItemView::SelectRows);
}
void BookmarkView::gotoBookmark(const QModelIndex &index) void BookmarkView::gotoBookmark(const QModelIndex &index)
{ {
Bookmark *bk = m_manager->bookmarkForIndex(index); Bookmark *bk = m_manager->bookmarkForIndex(index);
@@ -329,17 +318,6 @@ void BookmarkView::gotoBookmark(const QModelIndex &index)
m_manager->removeBookmark(bk); m_manager->removeBookmark(bk);
} }
////
// BookmarkContext
////
BookmarkContext::BookmarkContext(QWidget *widget)
: Core::IContext(widget)
{
setWidget(widget);
setContext(Core::Context(Constants::BOOKMARKS_CONTEXT));
}
//// ////
// BookmarkManager // BookmarkManager
//// ////
@@ -414,15 +392,15 @@ QVariant BookmarkManager::data(const QModelIndex &index, int role) const
if (role == BookmarkManager::Filename) if (role == BookmarkManager::Filename)
return m_bookmarksList.at(index.row())->fileName(); return m_bookmarksList.at(index.row())->fileName();
else if (role == BookmarkManager::LineNumber) if (role == BookmarkManager::LineNumber)
return m_bookmarksList.at(index.row())->lineNumber(); return m_bookmarksList.at(index.row())->lineNumber();
else if (role == BookmarkManager::Directory) if (role == BookmarkManager::Directory)
return m_bookmarksList.at(index.row())->path(); return m_bookmarksList.at(index.row())->path();
else if (role == BookmarkManager::LineText) if (role == BookmarkManager::LineText)
return m_bookmarksList.at(index.row())->lineText(); return m_bookmarksList.at(index.row())->lineText();
else if (role == BookmarkManager::Note) if (role == BookmarkManager::Note)
return m_bookmarksList.at(index.row())->note(); return m_bookmarksList.at(index.row())->note();
else if (role == Qt::ToolTipRole) if (role == Qt::ToolTipRole)
return QDir::toNativeSeparators(m_bookmarksList.at(index.row())->filePath()); return QDir::toNativeSeparators(m_bookmarksList.at(index.row())->filePath());
return QVariant(); return QVariant();
@@ -521,20 +499,17 @@ void BookmarkManager::removeBookmark(Bookmark *bookmark)
saveBookmarks(); saveBookmarks();
} }
Bookmark *BookmarkManager::bookmarkForIndex(const QModelIndex &index) Bookmark *BookmarkManager::bookmarkForIndex(const QModelIndex &index) const
{ {
if (!index.isValid() || index.row() >= m_bookmarksList.size()) if (!index.isValid() || index.row() >= m_bookmarksList.size())
return 0; return 0;
return m_bookmarksList.at(index.row()); return m_bookmarksList.at(index.row());
} }
bool BookmarkManager::gotoBookmark(Bookmark *bookmark) bool BookmarkManager::gotoBookmark(Bookmark *bookmark)
{ {
if (ITextEditor *editor = qobject_cast<ITextEditor *>(EditorManager::openEditorAt(bookmark->filePath(), if (IEditor *editor = EditorManager::openEditorAt(bookmark->filePath(), bookmark->lineNumber()))
bookmark->lineNumber()))) { return editor->currentLine() == bookmark->lineNumber();
return (editor->currentLine() == bookmark->lineNumber());
}
return false; return false;
} }
@@ -807,12 +782,12 @@ void BookmarkManager::saveBookmarks()
{ {
QStringList list; QStringList list;
foreach (const Bookmark *bookmark, m_bookmarksList) foreach (const Bookmark *bookmark, m_bookmarksList)
list << bookmarkToString(bookmark); list << bookmarkToString(bookmark);
SessionManager::setValue(QLatin1String("Bookmarks"), list); SessionManager::setValue(QLatin1String("Bookmarks"), list);
} }
void BookmarkManager::operateTooltip(TextEditor::ITextEditor *textEditor, const QPoint &pos, Bookmark *mark) void BookmarkManager::operateTooltip(ITextEditor *textEditor, const QPoint &pos, Bookmark *mark)
{ {
if (!mark) if (!mark)
return; return;
@@ -834,15 +809,15 @@ void BookmarkManager::loadBookmarks()
updateActionStatus(); updateActionStatus();
} }
void BookmarkManager::handleBookmarkRequest(TextEditor::ITextEditor *textEditor, void BookmarkManager::handleBookmarkRequest(ITextEditor *textEditor,
int line, int line,
TextEditor::ITextEditor::MarkRequestKind kind) ITextEditor::MarkRequestKind kind)
{ {
if (kind == TextEditor::ITextEditor::BookmarkRequest && textEditor->document()) if (kind == ITextEditor::BookmarkRequest && textEditor->document())
toggleBookmark(textEditor->document()->filePath(), line); toggleBookmark(textEditor->document()->filePath(), line);
} }
void BookmarkManager::handleBookmarkTooltipRequest(TextEditor::ITextEditor *textEditor, const QPoint &pos, void BookmarkManager::handleBookmarkTooltipRequest(ITextEditor *textEditor, const QPoint &pos,
int line) int line)
{ {
if (textEditor->document()) { if (textEditor->document()) {
@@ -881,8 +856,7 @@ QKeySequence BookmarkViewFactory::activationSequence() const
Core::NavigationView BookmarkViewFactory::createWidget() Core::NavigationView BookmarkViewFactory::createWidget()
{ {
BookmarkView *bookmarkView = new BookmarkView(); BookmarkView *bookmarkView = new BookmarkView(m_manager);
bookmarkView->setModel(m_manager);
Core::NavigationView view; Core::NavigationView view;
view.widget = bookmarkView; view.widget = bookmarkView;
return view; return view;

View File

@@ -63,7 +63,7 @@ public:
void updateBookmark(Bookmark *bookmark); void updateBookmark(Bookmark *bookmark);
void removeBookmark(Bookmark *bookmark); // Does not remove the mark void removeBookmark(Bookmark *bookmark); // Does not remove the mark
void removeAllBookmarks(); void removeAllBookmarks();
Bookmark *bookmarkForIndex(const QModelIndex &index); Bookmark *bookmarkForIndex(const QModelIndex &index) const;
enum State { NoBookMarks, HasBookMarks, HasBookmarksInDocument }; enum State { NoBookMarks, HasBookMarks, HasBookmarksInDocument };
State state() const; State state() const;
@@ -118,7 +118,7 @@ private slots:
private: private:
void documentPrevNext(bool next); void documentPrevNext(bool next);
Bookmark* findBookmark(const QString &path, const QString &fileName, int lineNumber); Bookmark *findBookmark(const QString &path, const QString &fileName, int lineNumber);
void addBookmark(Bookmark *bookmark, bool userset = true); void addBookmark(Bookmark *bookmark, bool userset = true);
void addBookmark(const QString &s); void addBookmark(const QString &s);
static QString bookmarkToString(const Bookmark *b); static QString bookmarkToString(const Bookmark *b);
@@ -139,58 +139,60 @@ private:
class BookmarkView : public Utils::ListView class BookmarkView : public Utils::ListView
{ {
Q_OBJECT Q_OBJECT
public: public:
BookmarkView(QWidget *parent = 0); explicit BookmarkView(BookmarkManager *manager);
~BookmarkView(); ~BookmarkView();
void setModel(QAbstractItemModel *model);
public slots: public slots:
void gotoBookmark(const QModelIndex &index); void gotoBookmark(const QModelIndex &index);
protected slots: protected slots:
void removeFromContextMenu(); void removeFromContextMenu();
void removeAll(); void removeAll();
protected: protected:
void contextMenuEvent(QContextMenuEvent *event); void contextMenuEvent(QContextMenuEvent *event);
void removeBookmark(const QModelIndex &index); void removeBookmark(const QModelIndex &index);
void keyPressEvent(QKeyEvent *event); void keyPressEvent(QKeyEvent *event);
private: private:
BookmarkContext *m_bookmarkContext; Core::IContext *m_bookmarkContext;
QModelIndex m_contextMenuIndex; QModelIndex m_contextMenuIndex;
BookmarkManager *m_manager; BookmarkManager *m_manager;
}; };
class BookmarkContext : public Core::IContext
{
public:
BookmarkContext(QWidget *widget);
};
class BookmarkViewFactory : public Core::INavigationWidgetFactory class BookmarkViewFactory : public Core::INavigationWidgetFactory
{ {
Q_OBJECT Q_OBJECT
public: public:
BookmarkViewFactory(BookmarkManager *bm); BookmarkViewFactory(BookmarkManager *bm);
private:
QString displayName() const; QString displayName() const;
int priority() const; int priority() const;
Core::Id id() const; Core::Id id() const;
QKeySequence activationSequence() const; QKeySequence activationSequence() const;
Core::NavigationView createWidget(); Core::NavigationView createWidget();
private:
BookmarkManager *m_manager; BookmarkManager *m_manager;
}; };
class BookmarkDelegate : public QStyledItemDelegate class BookmarkDelegate : public QStyledItemDelegate
{ {
Q_OBJECT Q_OBJECT
public: public:
BookmarkDelegate(QObject * parent = 0); BookmarkDelegate(QObject *parent = 0);
~BookmarkDelegate();
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
private: private:
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
void generateGradientPixmap(int width, int height, const QColor &color, bool selected) const; void generateGradientPixmap(int width, int height, const QColor &color, bool selected) const;
mutable QPixmap *m_normalPixmap;
mutable QPixmap *m_selectedPixmap; mutable QPixmap m_normalPixmap;
mutable QPixmap m_selectedPixmap;
}; };
} // namespace Internal } // namespace Internal

View File

@@ -52,13 +52,10 @@ using namespace Bookmarks::Constants;
using namespace Bookmarks::Internal; using namespace Bookmarks::Internal;
using namespace TextEditor; using namespace TextEditor;
BookmarksPlugin *BookmarksPlugin::m_instance = 0;
BookmarksPlugin::BookmarksPlugin() BookmarksPlugin::BookmarksPlugin()
: m_bookmarkManager(0), : m_bookmarkManager(0),
m_bookmarkMarginActionLineNumber(0) m_bookmarkMarginActionLineNumber(0)
{ {
m_instance = this;
} }
void BookmarksPlugin::extensionsInitialized() void BookmarksPlugin::extensionsInitialized()

View File

@@ -58,8 +58,6 @@ public:
BookmarksPlugin(); BookmarksPlugin();
~BookmarksPlugin(); ~BookmarksPlugin();
static BookmarksPlugin *instance() { return m_instance; }
bool initialize(const QStringList &arguments, QString *errorMessage); bool initialize(const QStringList &arguments, QString *errorMessage);
void extensionsInitialized(); void extensionsInitialized();