File indexing completed on 2024-04-28 05:43:24

0001 /***************************************************************************
0002  *   Copyright (C) 2002 Lucijan Busch <lucijan@gmx.at>                     *
0003  *   Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>            *
0004  *   Copyright (C) 2004 Jaroslaw Staniek <js@iidea.pl>                     *
0005  *   Copyright (C) 2006 David Saxton <david@bluehaze.org>                  *
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or modify  *
0008  *   it under the terms of the GNU General Public License as published by  *
0009  *   the Free Software Foundation; either version 2 of the License, or     *
0010  *   (at your option) any later version.                                   *
0011  ***************************************************************************/
0012 
0013 #include "propertyeditoritem.h"
0014 #include "drawpart.h"
0015 #include "propertyeditor.h"
0016 
0017 #include <KLocalizedString>
0018 
0019 #include <QColor>
0020 #include <QCursor>
0021 #include <QFont>
0022 #include <QPainter>
0023 #include <QPixmap>
0024 #include <QPoint>
0025 #include <QSize>
0026 
0027 #include <ktechlab_debug.h>
0028 
0029 // BEGIN Class PropertyEditorItem
0030 PropertyEditorItem::PropertyEditorItem(PropertyEditorItem * /*par*/, Property *property)
0031     : QTableWidgetItem(property->editorCaption() /*, property->displayString() */)
0032 {
0033     // setParent(par); // table takes ownership of the item
0034     // setText(property->editorCaption()); // need to set 2 items for each property
0035     // setExpandable( false ); // TODO
0036 
0037     m_property = property;
0038     // connect( m_property, SIGNAL(valueChanged( QVariant, QVariant )), this, SLOT(propertyValueChanged()) );
0039 
0040     // updateValue(); // need to set 2 items for each property
0041 
0042     // 3 rows per item is enough?
0043     //  setMultiLinesEnabled( true ); // TODO
0044     //  setHeight(static_cast<PropertyEditor*>(listView())->baseRowHeight()*3);
0045 }
0046 
0047 PropertyEditorItem::PropertyEditorItem(QTableWidget *parent, const QString &text)
0048     : QTableWidgetItem(text)
0049 {
0050     setParent(parent);
0051     setText(text);
0052 
0053     m_property = nullptr;
0054     setFlags(flags() &= (~Qt::ItemIsSelectable));
0055     // setSelectable(false);
0056     // setOpen(true);
0057 
0058     // 3 rows per item is enough?
0059     //  setMultiLinesEnabled( true );   // TODO
0060     //  setHeight(static_cast<PropertyEditor*>(par)->baseRowHeight()*3);
0061 }
0062 
0063 void PropertyEditorItem::propertyValueChanged()
0064 {
0065     setText(m_property->displayString());
0066 }
0067 
0068 #if 0 // 2018.08.13 - moved to property editor
0069 void PropertyEditorItem::paintCell(QPainter *p, const QColorGroup & cg, int column, int width, int align)
0070 {
0071     if ( depth() == 0 )
0072         return;
0073     
0074     int margin = listView()->itemMargin();
0075     
0076     QColor bgColor = cg.background(); // backgroundColor(0); // 2018.06.02 - is this better?
0077     
0078     if(column == 1)
0079     {
0080         switch(m_property->type())
0081         {
0082 //          case QVariant::Pixmap:
0083 //          {
0084 //              p->fillRect(0,0,width,height(),QBrush(backgroundColor()));
0085 //              p->drawPixmap(margin, margin, m_property->value().toPixmap());
0086 //              break;
0087 //          }
0088             
0089             case Variant::Type::Color:
0090             {
0091                 p->fillRect(0,0,width,height(), QBrush(bgColor));
0092                 //QColor ncolor = m_property->value().toColor();
0093                 QColor ncolor = m_property->value().value<QColor>();
0094                 p->setBrush(ncolor);
0095                 p->drawRect(margin, margin, width - 2*margin, height() - 2*margin);
0096                 QColorGroup nGroup(cg);
0097                 break;
0098             }
0099             
0100             case Variant::Type::Bool:
0101             {
0102                 p->fillRect(0,0,width,height(), QBrush(bgColor));
0103                 if(m_property->value().toBool())
0104                 {
0105                     p->drawPixmap(margin, height()/2 -8, SmallIcon("dialog-ok"));
0106                     p->drawText(QRect(margin+20,0,width,height()-1), Qt::AlignVCenter, i18n("Yes"));
0107                 }
0108                 else
0109                 {
0110                     p->drawPixmap(margin, height()/2 -8, SmallIcon("dialog-cancel"));
0111                     p->drawText(QRect(margin+20,0,width,height()-1), Qt::AlignVCenter, i18n("No"));
0112                 }
0113                 break;
0114             }
0115             
0116             case Variant::Type::PenStyle:
0117             {
0118                 p->fillRect(0,0,width,height(), QBrush(bgColor));
0119                 
0120                 Qt::PenStyle style = DrawPart::nameToPenStyle( m_property->value().toString() );
0121                 int penWidth = 3;
0122                 QPen pen( Qt::black, penWidth, style );
0123                 p->setPen( pen );
0124                 p->drawLine( height()/2, height()/2-1, width-height()/2, height()/2-1 );
0125                 break;
0126             }
0127 
0128 #if 0
0129             case Variant::Type::PenCapStyle:
0130             {
0131                 p->fillRect(0,0,width,height(), QBrush(bgColor));
0132                 
0133                 PenCapStyle style = DrawPart::nameToPenCapStyle( m_property->value().toString() );
0134                 int penWidth = 6;
0135                 QPen pen( black, penWidth, SolidLine, style, MiterJoin );
0136                 p->setPen( pen );
0137                 p->drawLine( width/2-10, height()/2-2, width/2+10, height()/2-2 );
0138                 break;
0139             }
0140 #endif
0141             
0142             case Variant::Type::None:
0143             case Variant::Type::Int:
0144             case Variant::Type::Raw:
0145             case Variant::Type::Double:
0146             case Variant::Type::String:
0147             case Variant::Type::Multiline:
0148             case Variant::Type::RichText:
0149             case Variant::Type::Select:
0150             case Variant::Type::Combo:
0151             case Variant::Type::FileName:
0152             case Variant::Type::VarName:
0153             case Variant::Type::PenCapStyle:
0154             case Variant::Type::Port:
0155             case Variant::Type::Pin:
0156             case Variant::Type::SevenSegment:
0157             case Variant::Type::KeyPad:
0158             {
0159                 Q3ListViewItem::paintCell(p, cg, column, width, align);
0160                 break;
0161             }
0162         }
0163     }
0164     else
0165     {
0166         if(isSelected())
0167         {
0168             p->fillRect(0,0,width, height(), QBrush(cg.highlight()));
0169             p->setPen(cg.highlightedText());
0170         }
0171         else
0172             p->fillRect(0,0,width, height(), QBrush(bgColor));
0173 
0174         QFont f = listView()->font();
0175         p->save();
0176         
0177         if ( m_property->changed() )
0178             f.setBold(true);
0179         
0180         p->setFont(f);
0181         p->drawText(QRect(margin,0,width, height()-1), Qt::AlignVCenter, text(0));
0182         p->restore();
0183 
0184         p->setPen( QColor(200,200,200) ); //like in table view
0185         p->drawLine(width-1, 0, width-1, height()-1);
0186     }
0187 
0188     p->setPen( QColor(200,200,200) ); //like in t.v.
0189     p->drawLine(-50, height()-1, width, height()-1 );
0190 }
0191 #endif
0192 
0193 // void PropertyEditorItem::setup()
0194 // {
0195 //  Q3ListViewItem::setup();
0196 //  if ( depth() == 0 )
0197 //      setHeight(0);
0198 // }
0199 
0200 PropertyEditorItem::~PropertyEditorItem()
0201 {
0202 }
0203 
0204 void PropertyEditorItem::updateValue(bool alsoParent)
0205 {
0206     QString text;
0207     if (m_property) {
0208         text = m_property->displayString();
0209     }
0210     qCDebug(KTL_LOG) << "text= " << text;
0211     setText(text);
0212     if (alsoParent && QObject::parent())
0213         static_cast<PropertyEditorItem *>(QObject::parent())->updateValue();
0214 }
0215 
0216 // void PropertyEditorItem::paintFocus ( QPainter * , const QColorGroup & , const QRect &  )
0217 // {
0218 // }
0219 // END class PropertyEditorItem
0220 
0221 #include "moc_propertyeditoritem.cpp"