Crossfire Server, Trunk
CREMultilineItemDelegate.cpp
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 2022 the Crossfire Development Team
5  *
6  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
7  * welcome to redistribute it under certain conditions. For details, please
8  * see COPYING and LICENSE.
9  *
10  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
11  */
12 
13 #include <QTextEdit>
14 #include <qstyleditemdelegate.h>
16 
17 CREMultilineItemDelegate::CREMultilineItemDelegate(QObject* parent, bool asStringList, bool trimEmpty)
18 : QStyledItemDelegate(parent), myAsStringList(asStringList), myTrimEmpty(trimEmpty)
19 {
20 }
21 
23 {
24 }
25 
26 QWidget* CREMultilineItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/) const
27 {
28  QTextEdit* edit = new QTextEdit(parent);
29  edit->setAcceptRichText(false);
30  return edit;
31 }
32 
33 void CREMultilineItemDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
34 {
35  if (!index.isValid())
36  return;
37  QTextEdit* edit = qobject_cast<QTextEdit*>(editor);
38  if (edit == NULL)
39  return;
40 
41  if (myAsStringList)
42  edit->setPlainText(index.model()->data(index, Qt::EditRole).value<QStringList>().join("\n"));
43  else
44  edit->setPlainText(index.model()->data(index, Qt::EditRole).toString());
45 }
46 
47 QStringList convert(const QString& source, bool trimEmpty)
48 {
49  QStringList value;
50  for (QString line : source.split("\n"))
51  {
52  if (!line.isEmpty() || !trimEmpty)
53  value.append(line);
54  }
55  return value;
56 }
57 
58 void CREMultilineItemDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
59 {
60  if (!index.isValid())
61  return;
62  QTextEdit* edit = qobject_cast<QTextEdit*>(editor);
63  if (edit == NULL)
64  return;
65 
66  if (myAsStringList)
67  model->setData(index, convert(edit->toPlainText(), myTrimEmpty));
68  else
69  model->setData(index, edit->toPlainText());
70 }
CREMultilineItemDelegate::~CREMultilineItemDelegate
virtual ~CREMultilineItemDelegate()
Definition: CREMultilineItemDelegate.cpp:22
CREMultilineItemDelegate::setEditorData
virtual void setEditorData(QWidget *editor, const QModelIndex &index) const override
Definition: CREMultilineItemDelegate.cpp:33
CREMultilineItemDelegate::CREMultilineItemDelegate
CREMultilineItemDelegate(QObject *parent, bool asStringList=true, bool trimEmpty=false)
Definition: CREMultilineItemDelegate.cpp:17
is_valid_types_gen.line
line
Definition: is_valid_types_gen.py:34
CREMultilineItemDelegate::createEditor
virtual QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Definition: CREMultilineItemDelegate.cpp:26
convert
QStringList convert(const QString &source, bool trimEmpty)
Definition: CREMultilineItemDelegate.cpp:47
CREMultilineItemDelegate.h
CREMultilineItemDelegate::myTrimEmpty
bool myTrimEmpty
Definition: CREMultilineItemDelegate.h:37
autojail.value
value
Definition: autojail.py:6
npc_dialog.index
int index
Definition: npc_dialog.py:102
CREMultilineItemDelegate::myAsStringList
bool myAsStringList
Definition: CREMultilineItemDelegate.h:36
CREMultilineItemDelegate::setModelData
virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
Definition: CREMultilineItemDelegate.cpp:58