Commit 06112d13 authored by hjk's avatar hjk
Browse files

Bookmarks: Code cosmetics



Spacing, namespaces, indirection, unused code...

Change-Id: Id2b0dccf7be0fc3b78305e16447f97546c6234bf
Reviewed-by: default avatarChristian Stenger <christian.stenger@digia.com>
parent 272176ca
......@@ -66,16 +66,10 @@ namespace Bookmarks {
namespace Internal {
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
{
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
{
QColor c = color;
c.setAlpha(0);
QPixmap *pixmap = new QPixmap(width+1, height);
pixmap->fill(c);
QPixmap pixmap(width+1, height);
pixmap.fill(c);
QPainter painter(pixmap);
QPainter painter(&pixmap);
painter.setPen(Qt::NoPen);
QLinearGradient lg;
......@@ -107,7 +100,7 @@ void BookmarkDelegate::generateGradientPixmap(int width, int height, const QColo
lg.setColorAt(0, c);
lg.setColorAt(0.4, color);
painter.setBrush(lg);
painter.setBrush(lg);
painter.drawRect(0, 0, width+1, height);
if (selected)
......@@ -154,7 +147,7 @@ void BookmarkDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
// 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);
QString topRight = index.data(BookmarkManager::LineNumber).toString();
......@@ -162,7 +155,7 @@ void BookmarkDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
int fwidth = fm.width(topLeft);
if (fwidth + lwidth > opt.rect.width()) {
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
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();
}
BookmarkView::BookmarkView(QWidget *parent) :
Utils::ListView(parent),
m_bookmarkContext(new BookmarkContext(this)),
m_manager(0)
BookmarkView::BookmarkView(BookmarkManager *manager) :
m_bookmarkContext(new Core::IContext(this)),
m_manager(manager)
{
setWindowTitle(tr("Bookmarks"));
connect(this, SIGNAL(clicked(QModelIndex)),
this, SLOT(gotoBookmark(QModelIndex)));
connect(this, SIGNAL(activated(QModelIndex)),
this, SLOT(gotoBookmark(QModelIndex)));
m_bookmarkContext->setWidget(this);
m_bookmarkContext->setContext(Core::Context(Constants::BOOKMARKS_CONTEXT));
ICore::addContextObject(m_bookmarkContext);
Utils::ListView::setModel(manager);
setItemDelegate(new BookmarkDelegate(this));
setFrameStyle(QFrame::NoFrame);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
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()
......@@ -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)
{
Bookmark *bk = m_manager->bookmarkForIndex(index);
......@@ -329,17 +318,6 @@ void BookmarkView::gotoBookmark(const QModelIndex &index)
m_manager->removeBookmark(bk);
}
////
// BookmarkContext
////
BookmarkContext::BookmarkContext(QWidget *widget)
: Core::IContext(widget)
{
setWidget(widget);
setContext(Core::Context(Constants::BOOKMARKS_CONTEXT));
}
////
// BookmarkManager
////
......@@ -414,15 +392,15 @@ QVariant BookmarkManager::data(const QModelIndex &index, int role) const
if (role == BookmarkManager::Filename)
return m_bookmarksList.at(index.row())->fileName();
else if (role == BookmarkManager::LineNumber)
if (role == BookmarkManager::LineNumber)
return m_bookmarksList.at(index.row())->lineNumber();
else if (role == BookmarkManager::Directory)
if (role == BookmarkManager::Directory)
return m_bookmarksList.at(index.row())->path();
else if (role == BookmarkManager::LineText)
if (role == BookmarkManager::LineText)
return m_bookmarksList.at(index.row())->lineText();
else if (role == BookmarkManager::Note)
if (role == BookmarkManager::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 QVariant();
......@@ -521,20 +499,17 @@ void BookmarkManager::removeBookmark(Bookmark *bookmark)
saveBookmarks();
}
Bookmark *BookmarkManager::bookmarkForIndex(const QModelIndex &index)
Bookmark *BookmarkManager::bookmarkForIndex(const QModelIndex &index) const
{
if (!index.isValid() || index.row() >= m_bookmarksList.size())
return 0;
return m_bookmarksList.at(index.row());
}
bool BookmarkManager::gotoBookmark(Bookmark *bookmark)
{
if (ITextEditor *editor = qobject_cast<ITextEditor *>(EditorManager::openEditorAt(bookmark->filePath(),
bookmark->lineNumber()))) {
return (editor->currentLine() == bookmark->lineNumber());
}
if (IEditor *editor = EditorManager::openEditorAt(bookmark->filePath(), bookmark->lineNumber()))
return editor->currentLine() == bookmark->lineNumber();
return false;
}
......@@ -807,12 +782,12 @@ void BookmarkManager::saveBookmarks()
{
QStringList list;
foreach (const Bookmark *bookmark, m_bookmarksList)
list << bookmarkToString(bookmark);
list << bookmarkToString(bookmark);
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)
return;
......@@ -834,15 +809,15 @@ void BookmarkManager::loadBookmarks()
updateActionStatus();
}
void BookmarkManager::handleBookmarkRequest(TextEditor::ITextEditor *textEditor,
void BookmarkManager::handleBookmarkRequest(ITextEditor *textEditor,
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);
}
void BookmarkManager::handleBookmarkTooltipRequest(TextEditor::ITextEditor *textEditor, const QPoint &pos,
void BookmarkManager::handleBookmarkTooltipRequest(ITextEditor *textEditor, const QPoint &pos,
int line)
{
if (textEditor->document()) {
......@@ -881,8 +856,7 @@ QKeySequence BookmarkViewFactory::activationSequence() const
Core::NavigationView BookmarkViewFactory::createWidget()
{
BookmarkView *bookmarkView = new BookmarkView();
bookmarkView->setModel(m_manager);
BookmarkView *bookmarkView = new BookmarkView(m_manager);
Core::NavigationView view;
view.widget = bookmarkView;
return view;
......
......@@ -63,7 +63,7 @@ public:
void updateBookmark(Bookmark *bookmark);
void removeBookmark(Bookmark *bookmark); // Does not remove the mark
void removeAllBookmarks();
Bookmark *bookmarkForIndex(const QModelIndex &index);
Bookmark *bookmarkForIndex(const QModelIndex &index) const;
enum State { NoBookMarks, HasBookMarks, HasBookmarksInDocument };
State state() const;
......@@ -118,7 +118,7 @@ private slots:
private:
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(const QString &s);
static QString bookmarkToString(const Bookmark *b);
......@@ -139,58 +139,60 @@ private:
class BookmarkView : public Utils::ListView
{
Q_OBJECT
public:
BookmarkView(QWidget *parent = 0);
explicit BookmarkView(BookmarkManager *manager);
~BookmarkView();
void setModel(QAbstractItemModel *model);
public slots:
void gotoBookmark(const QModelIndex &index);
protected slots:
void removeFromContextMenu();
void removeAll();
protected:
void contextMenuEvent(QContextMenuEvent *event);
void removeBookmark(const QModelIndex &index);
void keyPressEvent(QKeyEvent *event);
private:
BookmarkContext *m_bookmarkContext;
Core::IContext *m_bookmarkContext;
QModelIndex m_contextMenuIndex;
BookmarkManager *m_manager;
};
class BookmarkContext : public Core::IContext
{
public:
BookmarkContext(QWidget *widget);
};
class BookmarkViewFactory : public Core::INavigationWidgetFactory
{
Q_OBJECT
public:
BookmarkViewFactory(BookmarkManager *bm);
private:
QString displayName() const;
int priority() const;
Core::Id id() const;
QKeySequence activationSequence() const;
Core::NavigationView createWidget();
private:
BookmarkManager *m_manager;
};
class BookmarkDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
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;
BookmarkDelegate(QObject *parent = 0);
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;
mutable QPixmap *m_normalPixmap;
mutable QPixmap *m_selectedPixmap;
mutable QPixmap m_normalPixmap;
mutable QPixmap m_selectedPixmap;
};
} // namespace Internal
......
......@@ -52,13 +52,10 @@ using namespace Bookmarks::Constants;
using namespace Bookmarks::Internal;
using namespace TextEditor;
BookmarksPlugin *BookmarksPlugin::m_instance = 0;
BookmarksPlugin::BookmarksPlugin()
: m_bookmarkManager(0),
m_bookmarkMarginActionLineNumber(0)
{
m_instance = this;
}
void BookmarksPlugin::extensionsInitialized()
......
......@@ -58,8 +58,6 @@ public:
BookmarksPlugin();
~BookmarksPlugin();
static BookmarksPlugin *instance() { return m_instance; }
bool initialize(const QStringList &arguments, QString *errorMessage);
void extensionsInitialized();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment