LeechCraft  0.6.70-18450-gabe19ee3b0
Modular cross-platform feature rich live environment.
tagseditdelegate.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Distributed under the Boost Software License, Version 1.0.
6  * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7  **********************************************************************/
8 
9 #include "tagseditdelegate.h"
10 #include <QApplication>
12 #include "tagscompleter.h"
13 #include "tagslineedit.h"
14 
15 namespace LC::Util
16 {
18  : QStyledItemDelegate { parent }
19  , ITM_ { itm }
20  {
21  }
22 
23  QWidget* TagsEditDelegate::createEditor (QWidget *parent, const QStyleOptionViewItem&, const QModelIndex& index) const
24  {
25  const auto edit = new TagsLineEdit { parent };
26  new TagsCompleter { edit };
27  edit->AddSelector ();
28  edit->setFrame (false);
29  setEditorData (edit, index);
30  return edit;
31  }
32 
33  void TagsEditDelegate::paint (QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
34  {
35  auto opt = option;
36  initStyleOption (&opt, index);
37  opt.text = ITM_.Join (index.data ().toStringList ());
38 
39  const auto style = opt.widget ? opt.widget->style () : QApplication::style ();
40  style->drawControl (QStyle::CE_ItemViewItem, &opt, painter);
41  }
42 
43  void TagsEditDelegate::setEditorData (QWidget *editor, const QModelIndex& index) const
44  {
45  const auto& tags = index.data (Qt::EditRole).toStringList ();
46  dynamic_cast<TagsLineEdit*> (editor)->setText (ITM_.Join (tags));
47  }
48 
49  void TagsEditDelegate::setModelData (QWidget *editor, QAbstractItemModel *model, const QModelIndex& index) const
50  {
51  const auto& tags = ITM_.Split (dynamic_cast<TagsLineEdit*> (editor)->text ());
52  model->setData (index, tags, Qt::EditRole);
53  }
54 }
void setModelData(QWidget *, QAbstractItemModel *, const QModelIndex &) const override
A line edit class suitable for use with TagsCompleter.
Definition: tagslineedit.h:28
Tags manager&#39;s interface.
Definition: itagsmanager.h:22
TagsEditDelegate(ITagsManager &, QObject *=nullptr)
virtual QString Join(const QStringList &tags) const =0
Joins the given tags into one string that&#39;s suitable to display to the user.
QWidget * createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const override
Completer suitable for tag completion.
Definition: tagscompleter.h:38
virtual QStringList Split(const QString &string) const =0
Splits the given string with tags to the list of the tags.
void paint(QPainter *, const QStyleOptionViewItem &, const QModelIndex &) const override
void setEditorData(QWidget *, const QModelIndex &) const override