LogMessageDelegate.cpp
Go to the documentation of this file.
1/*
2* This file is part of ArmarX.
3*
4* ArmarX is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License version 2 as
6* published by the Free Software Foundation.
7*
8* ArmarX is distributed in the hope that it will be useful, but
9* WITHOUT ANY WARRANTY; without even the implied warranty of
10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11* GNU General Public License for more details.
12*
13* You should have received a copy of the GNU General Public License
14* along with this program. If not, see <http://www.gnu.org/licenses/>.
15*
16* @package ArmarX::
17* @author Mirko Waechter ( mirko.waechter at kit dot edu)
18* @date 2012
19* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20* GNU General Public License
21*/
22
23#include "LogMessageDelegate.h"
24
25#include <algorithm>
26#include <iostream>
27
28#include <QPainter>
29#include <QPalette>
30#include <QTableView>
31#include <QTextEdit>
32
33#include "LogTableModel.h"
34
35namespace armarx
36{
37 LogMessageDelegate::LogMessageDelegate(QObject* parent) : QStyledItemDelegate(parent)
38 {
39 // _lineEdit = new QLabel();
40 // _lineEdit->setWordWrap(true);
41 }
42
44 {
45 // delete _lineEdit;
46 }
47
48 //void LogMessageDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
49 //{
50
51 // QString logMessageStr = index.data().toString();
52 // _lineEdit->setText(logMessageStr);
53 // int lines = std::count(logMessageStr.begin(), logMessageStr.end(), '\n')+1;
54 // if(lines > 5)
55 // lines = 5;
56 // QRect rect = option.rect;
57
58 // rect.setHeight(15*lines);
59 // _lineEdit->resize(rect.size());
60 // std::cout << "lines:" << lines << "old height: " << option.rect.height() << " new: " << rect.height() << " lineedit height: " << _lineEdit->height() << std::endl;
61 // // Change background color if the item is selected
62 // QPalette pal;
63 // if ((option.state & QStyle::State_Selected) == QStyle::State_Selected)
64 // {
65 // pal.setBrush(QPalette::Window, QBrush(QColor(Qt::lightGray)));
66 // }
67 // else
68 // {
69 // pal.setBrush(QPalette::Window, QBrush(QColor(Qt::transparent)));
70 // }
71 // _lineEdit->setPalette(pal);
72
73 // // Paint the widget now.
74 // painter->save();
75 // painter->translate(option.rect.topLeft());
76 // _lineEdit->render(painter);
77 // painter->restore();
78 //}
79
80 //QSize LogMessageDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
81 //{
82 // const int width = 350;
83
84 // const int height = 200;
85 // std::cout << "height: " << _lineEdit->height() << std::endl;
86 // return QSize(width, _lineEdit->height());
87 //}
88
89 QWidget*
91 const QStyleOptionViewItem& option,
92 const QModelIndex& index) const
93 {
94 if (index.column() == 4)
95 {
96 // QTableView* tableView = qobject_cast<QTableView*>(parent);
97 QTextEdit* editor = new QTextEdit(parent);
98 // editor->resize(100,100);
99 editor->adjustSize();
100 editor->setReadOnly(true);
101 // connect(this, SIGNAL(resizeRow(int)),
102 // tableView, SLOT(resizeRowToContents(int)));
103 // emit resizeRow(index.column());
104 // tableView->resizeRowToContents(index.column());
105 return editor;
106 }
107 else
108 {
109 return QStyledItemDelegate::createEditor(parent, option, index);
110 }
111 }
112
113 void
114 LogMessageDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
115 {
116 QTextEdit* textEdit = qobject_cast<QTextEdit*>(editor);
117 if (!textEdit)
118 {
119 QStyledItemDelegate::setEditorData(editor, index);
120 return;
121 }
122 textEdit->setText(index.data((int)LogTableModel::UserRoles::FullMsgRole).toString());
123 }
124
125 void
127 const QStyleOptionViewItem& option,
128 const QModelIndex& index) const
129 {
130 if (!qobject_cast<QTextEdit*>(editor))
131 {
132 QStyledItemDelegate::updateEditorGeometry(editor, option, index);
133 return;
134 }
135
136 // Anchor the overlay at the cell and size it to the message content instead of a
137 // fixed 700x300, clamped to the parent viewport so it never overflows the widget.
138 QRect r = option.rect;
139 int width = r.width();
140 int height = r.height();
141 if (auto* textEdit = qobject_cast<QTextEdit*>(editor))
142 {
143 const QSizeF docSize = textEdit->document()->size();
144 width = std::max(width, static_cast<int>(docSize.width()) + 20);
145 height = std::max(height, static_cast<int>(docSize.height()) + 10);
146 }
147 if (const QWidget* parent = editor->parentWidget())
148 {
149 width = std::min(width, parent->width() - r.x());
150 height = std::min(height, parent->height() - r.y());
151 }
152 r.setSize(QSize(width, height));
153 editor->setGeometry(r);
154 }
155
156 void
158 {
159 emit closeEditor(qobject_cast<QWidget*>(sender()));
160 }
161} // namespace armarx
uint8_t index
#define option(type, fn)
LogMessageDelegate(QObject *parent=0)
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
void setEditorData(QWidget *editor, const QModelIndex &index) const override
This file offers overloads of toIce() and fromIce() functions for STL container types.