11#include " buttondelegate.h"
22#include " displaydata.hpp"
33
4- #include < QApplication>
5- #include < QMouseEvent>
6- #include < QPainter>
74#include < QtWidgets>
85
96ButtonDelegate::ButtonDelegate (QObject *parent)
107 : QStyledItemDelegate(parent)
11- , m_buttonPtr(new QStyleOptionButton)
128{}
139
1410ButtonDelegate::~ButtonDelegate () {}
@@ -17,21 +13,28 @@ void ButtonDelegate::paint(QPainter *painter,
1713 const QStyleOptionViewItem &option,
1814 const QModelIndex &index) const
1915{
20- int w = qMin (option.rect .width (), option.rect .height ()) / 10.0 ;
21- m_buttonPtr->rect = option.rect .adjusted (w, w, -w, -w);
22- m_buttonPtr->text = index.model ()->data (index).toString ();
23- m_buttonPtr->state |= QStyle::State_Enabled;
16+ auto copyOption = option;
17+ initStyleOption (©Option, index);
2418
25- painter->save ();
19+ const auto *widget = option.widget ;
20+ auto *style = widget ? widget->style () : QApplication::style ();
2621
27- if (option.state & QStyle::State_Selected) {
28- painter->fillRect (option.rect , option.palette .highlight ());
29- painter->setBrush (option.palette .highlightedText ());
30- }
22+ auto rect = option.rect ;
23+ auto margin = qMin (rect.width (), rect.height ()) / 10.0 ;
3124
32- QPushButton button;
33- qApp->style ()->drawControl (QStyle::CE_PushButton, m_buttonPtr.data (), painter, &button);
25+ QStyleOptionButton optionButton;
26+ if (widget) {
27+ optionButton.initFrom (widget);
28+ }
29+ optionButton.state = copyOption.state ;
30+ optionButton.direction = copyOption.direction ;
31+ optionButton.rect = rect.adjusted (margin, margin, -margin, -margin);
32+ optionButton.fontMetrics = copyOption.fontMetrics ;
33+ optionButton.palette = copyOption.palette ;
34+ optionButton.text = copyOption.text ;
3435
36+ painter->save ();
37+ style->drawControl (QStyle::CE_PushButton, &optionButton, painter, widget);
3538 painter->restore ();
3639}
3740
@@ -40,38 +43,33 @@ bool ButtonDelegate::editorEvent(QEvent *event,
4043 const QStyleOptionViewItem &option,
4144 const QModelIndex &index)
4245{
43- int w = qMin (option.rect .width (), option.rect .height ()) / 10.0 ;
44-
46+ auto type = event->type ();
4547 switch (event->type ()) {
46- case QEvent::MouseButtonPress: {
47- QMouseEvent *mouseEvent = (QMouseEvent *) event;
48- if (option.rect .adjusted (w, w, -w, -w).contains (mouseEvent->pos ())) {
49- m_buttonPtr->state |= QStyle::State_Sunken;
50- }
51- } break ;
5248 case QEvent::MouseButtonRelease: {
53- QMouseEvent *mouseEvent = (QMouseEvent *) event;
54- if (option.rect .adjusted (w, w, -w, -w).contains (mouseEvent->pos ())) {
55- m_buttonPtr->state &= (~QStyle::State_Sunken);
49+ auto data = model->data (index, Qt::UserRole).value <DisplayInfo>();
50+ auto details = tr (" Title: %1\n Number: %2\n State: %3\n Process: %4\n RichText: %5" )
51+ .arg (data.title ())
52+ .arg (data.number ())
53+ .arg (data.state ())
54+ .arg (data.process ())
55+ .arg (data.richText () + " x" );
5656
57- auto data = model->data (index, Qt::UserRole).value <DisplayInfo>();
58- auto details = tr (" Title: %1\n Number: %2\n State: %3\n Process: %4\n RichText: %5" )
59- .arg (data.title ())
60- .arg (data.number ())
61- .arg (data.state ())
62- .arg (data.process ())
63- .arg (data.richText () + " x" );
64-
65- auto w = qobject_cast<QWidget *>(model->parent ());
66- if (w) {
67- QDialog dialog (w);
68- QHBoxLayout *layout = new QHBoxLayout (&dialog);
69- layout->addWidget (new QLabel (details, &dialog));
70- dialog.exec ();
57+ auto *widget = const_cast <QWidget *>(option.widget );
58+ if (widget) {
59+ while (widget->parentWidget ()) {
60+ widget = widget->parentWidget ();
7161 }
62+
63+ QDialog dialog (widget);
64+ dialog.setMinimumSize (400 , 250 );
65+ auto *label = new QLabel (details, &dialog);
66+ label->setWordWrap (true );
67+ auto *layout = new QHBoxLayout (&dialog);
68+ layout->addWidget (label);
69+ dialog.exec ();
7270 }
7371 } break ;
7472 default : break ;
7573 }
76- return true ;
74+ return QStyledItemDelegate::editorEvent (event, model, option, index) ;
7775}
0 commit comments