FEUITable.cpp
Go to the documentation of this file.
00001 /*-------------------------------------------------------
00002 |                                                       |
00003 |                  FEUITable.cpp                        |
00004 |                                                       |
00005 ---------------------------------------------------------
00006 
00007 Copyright  2007         FEIG ELECTRONIC GmbH, All Rights Reserved.
00008                                                 Lange Strasse 4
00009                                                 D-35781 Weilburg
00010                                                 Federal Republic of Germany
00011                                                 phone    : +49 6471 31090
00012                                                 fax      : +49 6471 310999
00013                                                 e-mail   : info@feig.de
00014                                                 Internet : http://www.feig.de
00015                                         
00016 Author                  :       Benjamin Stadin
00017 Begin                   :       13.12.2006
00018 
00019 Version                 :       01.00.00 / 11.01.2007 / Benjamin Stadin
00020 
00021 Operation Systems       :       Linux
00022 */
00023 
00024 #include <qheaderview.h>
00025 #include "FEUITable.h"
00026 
00027 FEUITableDelegate::FEUITableDelegate(QObject *parent)
00028     : QItemDelegate(parent) {}
00029 
00030 QWidget *FEUITableDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem&,
00031     const QModelIndex &index) const
00032 {
00033     QLineEdit *editor = new QLineEdit(parent);
00034     editor->setReadOnly(true);
00035 
00036     connect(editor, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
00037     return editor;
00038 }
00039 
00040 void FEUITableDelegate::commitAndCloseEditor()
00041 {
00042     QLineEdit *editor = qobject_cast<QLineEdit *>(sender());
00043     emit commitData(editor);
00044     emit closeEditor(editor);
00045 }
00046 
00047 void FEUITableDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
00048 {
00049     QLineEdit *edit = qobject_cast<QLineEdit *>(editor);
00050     if (edit) 
00051     {
00052         edit->setText(index.model()->data(index, Qt::EditRole).toString());
00053     }
00054 }
00055 
00056 void FEUITableDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
00057     const QModelIndex &index) const
00058 {
00059     QLineEdit *edit = qobject_cast<QLineEdit *>(editor);
00060     if (edit) 
00061     {
00062         model->setData(index, edit->text());
00063     }
00064 }
00065 
00066 FEUITableItem::FEUITableItem()
00067     : QTableWidgetItem() {}
00068 
00069 FEUITableItem::FEUITableItem(const QString &text)
00070     : QTableWidgetItem(text) {}
00071 
00072 QTableWidgetItem *FEUITableItem::clone() const
00073 {
00074     FEUITableItem *item = new FEUITableItem();
00075     *item = *this;
00076     return item;
00077 }
00078 
00079 QVariant FEUITableItem::data(int role) const
00080 {
00081     if (role == Qt::DisplayRole)
00082         return display();
00083 
00084     QString t = display().toString();
00085     bool isNumber = false;
00086     int number = t.toInt(&isNumber);
00087 
00088     if (role == Qt::TextColorRole) {
00089         if (!isNumber)
00090             return qVariantFromValue(QColor(Qt::black));
00091         else if (number < 0)
00092             return qVariantFromValue(QColor(Qt::red));
00093         return qVariantFromValue(QColor(Qt::blue));
00094     }
00095 
00096     if (role == Qt::TextAlignmentRole)
00097         if (!t.isEmpty() && (t.at(0).isNumber() || t.at(0) == '-'))
00098             return (int)(Qt::AlignRight | Qt::AlignVCenter);
00099 
00100     return QTableWidgetItem::data(role);
00101 }
00102 
00103 void FEUITableItem::setData(int role, const QVariant &value)
00104 {
00105     QTableWidgetItem::setData(role, value);
00106     if (tableWidget())
00107         tableWidget()->viewport()->update();
00108 }
00109 
00110 QVariant FEUITableItem::display() const
00111 {
00112     return QVariant();
00113 }
00114 
00115 void FEUITable::newRow()
00116 {
00117         // add a row to the table
00118         int cnt = this->rowCount();
00119         cnt++;
00120         this->setRowCount(cnt);
00121 }
00122 
00123 int FEUITable::edit(int row, QString colByName, QString text)
00124 {
00125         int col = -1;
00126         
00127         if (row > this->rowCount()-1)
00128                 return -1;
00129          
00130         // Find column by text
00131         for (int i = 0; i < m_colList.size(); i++) 
00132         {
00133                 if (colByName == m_colList[i])
00134                 {
00135                         col = i;
00136                         break;
00137                 }
00138         }
00139         
00140         if (col >= 0)
00141         {
00142                 // set text for the column
00143                 QTableWidgetItem *item = this->item(row, col);
00144                 if (!item)
00145                         this->setItem(row, col, new QTableWidgetItem(text));
00146                 else
00147                         item->setText(text);
00148                 return 0;
00149         }
00150         else
00151                 return -1;
00152 }
00153 
00154 
00155 FEUITable::FEUITable(QStringList columns, QWidget *parent)
00156     : QTableWidget(parent)
00157 {
00158         // internal list to map colum names and index
00159         m_colList = columns;
00160         int cols = columns.size();
00161         
00162         QFont fnt;
00163         fnt.setFixedPitch(true);
00164         fnt.setStyleHint(QFont::System);
00165         // fixed width font
00166         fnt.setFamily("Misc Fixed");
00167         setFont(fnt);
00168 
00169         for (int c = 0; c < cols; ++c) 
00170         {
00171                 this->insertColumn(c);
00172                 QString colName;
00173                 colName = columns[c];
00174                 this->setHorizontalHeaderItem(c, new QTableWidgetItem(colName));
00175         }
00176         //setItemPrototype(item(rows - 1, cols - 1));
00177         this->setItemPrototype(item(1, 1));
00178         this->setItemDelegate(new FEUITableDelegate());
00179         #if !defined(QT_NO_DBUS) && defined(Q_OS_UNIX)
00180         new FEUITableAdaptor(this);
00181         #endif
00182         // hide the column index on the left
00183         QHeaderView *h = this->verticalHeader();
00184         h->hide();
00185         // stretch the horizontal index header to fill the complete widget
00186         h = this->horizontalHeader();
00187         h->stretchLastSection();
00188         //ResizeToContents
00189         h->setResizeMode(QHeaderView::Stretch);
00190         createActions();
00191         setupContextMenu();
00192 }
00193 
00194 FEUITable::~FEUITable()
00195 {
00196         //
00197 }
00198 
00199 void FEUITable::createActions()
00200 {
00201     clearAction = new QAction(tr("Clear"), this);
00202     clearAction->setShortcut(Qt::Key_Delete);
00203     connect(clearAction, SIGNAL(triggered()), this, SLOT(clear()));
00204 }
00205 
00206 void FEUITable::clear()
00207 {
00208         clearContents();
00209         setRowCount(0);
00210 }
00211 
00212 void FEUITable::setupContextMenu()
00213 {
00214     addAction(clearAction);
00215     setContextMenuPolicy(Qt::ActionsContextMenu);
00216 }
00217 
00218 void FEUITable::resizeEvent(QResizeEvent *event)
00219 {
00220         /*event->accept(); qt4.2.1
00221         resize(event->size());
00222         resize(event->size());*/
00223         QHeaderView *h = horizontalHeader();
00224         h->stretchLastSection();
00225 }


maggie_rfid_drivers
Author(s): Raul Perula-Martinez
autogenerated on Mon Sep 14 2015 03:05:30