[KLF Backend][KLF Tools][KLF Home]
KLatexFormula Project
src/klftools/klfpobjeditwidget.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *   file klfpobjeditwidget.cpp
00003  *   This file is part of the KLatexFormula Project.
00004  *   Copyright (C) 2012 by Philippe Faist
00005  *   philippe.faist at bluewin.ch
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************/
00022 /* $Id$ */
00023 
00024 
00025 #include <QAbstractItemModel>
00026 #include <QItemDelegate>
00027 
00028 #include <klfdebug.h>
00029 
00030 #include "klfpobj.h"
00031 #include "klfdatautil.h"
00032 
00033 #include "klfpobjeditwidget.h"
00034 #include "klfpobjeditwidget_p.h"
00035 
00036 
00037 
00038 // -----------------------------------------------
00039 
00040 
00041 KLFPObjModel::KLFPObjModel(KLFAbstractPropertizedObject *pobj, QObject *parent)
00042   : QAbstractItemModel(parent)
00043 {
00044   KLF_DEBUG_TIME_BLOCK(KLF_FUNC_NAME) ;
00045 
00046   KLF_INIT_PRIVATE(KLFPObjModel) ;
00047 
00048   setPObj(pobj);
00049 }
00050 
00051 KLFPObjModel::~KLFPObjModel()
00052 {
00053   KLF_DEBUG_TIME_BLOCK(KLF_FUNC_NAME) ;
00054 
00055   KLF_DELETE_PRIVATE ;
00056 }
00057 
00058 void KLFPObjModel::setPObj(KLFAbstractPropertizedObject *pobj)
00059 {
00060   KLF_DEBUG_BLOCK(KLF_FUNC_NAME);
00061   klfDbg("pobj="<<pobj) ;
00062 
00063   beginResetModel();
00064 
00065   d->pObj = pobj;
00066   if (d->pObj != NULL)
00067     d->propertyNames = d->pObj->propertyNameList();
00068   else
00069     d->propertyNames = QStringList();
00070 
00071   endResetModel();
00072 }
00073 
00074 KLFAbstractPropertizedObject * KLFPObjModel::pObj()
00075 {
00076   return d->pObj;
00077 }
00078 
00079 QVariant KLFPObjModel::data(const QModelIndex& index, int role) const
00080 {
00081   KLF_DEBUG_TIME_BLOCK(KLF_FUNC_NAME) ;
00082   klfDbg( "\tindex="<<index<<"; role="<<role ) ;
00083 
00084   if (!index.isValid()) {
00085     klfDbg("Invalid index.") ;
00086     return QVariant();
00087   }
00088 
00089   int n = index.row();
00090 
00091   if (index.column() < 0 || index.column() > 1 ||
00092       n < 0 || n >= d->propertyNames.count()) {
00093     // out of range
00094     klfWarning("Index out of range: "<<index) ;
00095     return QVariant();
00096   }
00097 
00098   if (index.column() == 0) {
00099     // property name
00100     if (role == Qt::ToolTipRole || role == Qt::DisplayRole) {
00101       // current contents
00102       return d->propertyNames[n];
00103     }
00104 
00105   } else {
00106     // property value
00107 
00108     if (role == Qt::ToolTipRole || role == Qt::DisplayRole) {
00109       return QVariant(klfSaveVariantToText(d->pObj->property(d->propertyNames[n])));
00110     }
00111     if (role == Qt::EditRole) {
00112       // current contents
00113       return d->pObj->property(d->propertyNames[n]);
00114     }
00115   }
00116 
00117   // by default
00118   return QVariant();
00119 }
00120 
00121 Qt::ItemFlags KLFPObjModel::flags(const QModelIndex& index) const
00122 {
00123   KLF_DEBUG_TIME_BLOCK(KLF_FUNC_NAME) ;
00124   klfDbg( "\tindex="<<index ) ;
00125 
00126   if (!index.isValid()) {
00127     klfDbg("Invalid index.") ;
00128     return 0;
00129   }
00130 
00131   int n = index.row();
00132 
00133   if (index.column() < 0 || index.column() > 1 ||
00134       n < 0 || n >= d->propertyNames.count()) {
00135     // out of range
00136     klfWarning("Index out of range: "<<index) ;
00137     return 0;
00138   }
00139 
00140   if (index.column() == 0) {
00141     // property name
00142     return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
00143   } else {
00144     // property value
00145     return Qt::ItemIsSelectable | Qt::ItemIsEnabled  | Qt::ItemIsEditable;
00146   }
00147 
00148   return 0;
00149 }
00150 
00151 bool KLFPObjModel::hasChildren(const QModelIndex &parent) const
00152 {
00153   if (!parent.isValid())
00154     return true;
00155   return false;
00156 }
00157 
00158 QVariant KLFPObjModel::headerData(int section, Qt::Orientation orientation, int role) const
00159 {
00160   if (orientation == Qt::Horizontal) {
00161     if (role == Qt::SizeHintRole) {
00162       if (section == 0) { // property name
00163         return QSize(100,30);
00164       } else { // property value
00165         return QSize(100,30);
00166       }
00167     }
00168     if (role == Qt::DisplayRole) {
00169       if (section == 0) {
00170         return tr("Property");
00171       } else {
00172         return tr("Value");
00173       }
00174     }
00175   }
00176   return QVariant();
00177 }
00178 
00179 bool KLFPObjModel::hasIndex(int row, int column, const QModelIndex &parent) const
00180 {
00181   if (column < 0 || column > 1)
00182     return false;
00183   if (row < 0 || row >= d->propertyNames.size())
00184     return false;
00185   if (parent.isValid())
00186     return false;
00187   return true;
00188 }
00189 
00190 QModelIndex KLFPObjModel::index(int row, int column, const QModelIndex &parent) const
00191 {
00192   if (!hasIndex(row, column, parent))
00193     return QModelIndex();
00194 
00195   return createIndex(row, column);
00196 }
00197 
00198 QModelIndex KLFPObjModel::parent(const QModelIndex &/*index*/) const
00199 {
00200   return QModelIndex();
00201 }
00202 
00203 int KLFPObjModel::rowCount(const QModelIndex &parent) const
00204 {
00205   if (parent.isValid())
00206     return 0;
00207   return d->propertyNames.size();
00208 }
00209 
00210 int KLFPObjModel::columnCount(const QModelIndex &parent) const
00211 {
00212   if (parent.isValid())
00213     return 0;
00214   return 2;
00215 }
00216 
00217 bool KLFPObjModel::canFetchMore(const QModelIndex& /*parent*/) const
00218 {
00219   return false;
00220 }
00221 
00222 void KLFPObjModel::fetchMore(const QModelIndex& /*parent*/)
00223 {
00224   return;
00225 }
00226 
00227 void KLFPObjModel::updateData()
00228 {
00229   setPObj(d->pObj);
00230 }
00231 
00232 
00233 // -------------------------------------------------
00234 
00235 
00236 struct KLFPObjEditWidgetPrivate {
00237   KLF_PRIVATE_HEAD(KLFPObjEditWidget) {
00238     model = NULL;
00239     delegate = NULL;
00240   }
00241 
00242   KLFPObjModel * model;
00243   QItemDelegate * delegate;
00244 };
00245 
00246 
00247 KLFPObjEditWidget::KLFPObjEditWidget(KLFAbstractPropertizedObject *pobj, QWidget *parent)
00248   : QTreeView(parent)
00249 {
00250   KLF_DEBUG_TIME_BLOCK(KLF_FUNC_NAME) ;
00251 
00252   KLF_INIT_PRIVATE(KLFPObjEditWidget) ;
00253 
00254   d->model = new KLFPObjModel(pobj, this);
00255   d->delegate = new QItemDelegate(this);
00256 
00257   setModel(d->model);
00258   setItemDelegate(d->delegate);
00259 }
00260 
00261 KLFPObjEditWidget::KLFPObjEditWidget(QWidget *parent)
00262   : QTreeView(parent)
00263 {
00264   KLF_DEBUG_TIME_BLOCK(KLF_FUNC_NAME) ;
00265 
00266   KLF_INIT_PRIVATE(KLFPObjEditWidget) ;
00267 
00268   d->model = new KLFPObjModel(NULL, this);
00269   d->delegate = new QItemDelegate(this);
00270 
00271   setModel(d->model);
00272   setItemDelegate(d->delegate);
00273 }
00274 
00275 
00276 KLFPObjEditWidget::~KLFPObjEditWidget()
00277 {
00278   KLF_DEBUG_TIME_BLOCK(KLF_FUNC_NAME) ;
00279 
00280   KLF_DELETE_PRIVATE ;
00281 }
00282 
00283 void KLFPObjEditWidget::setPObj(KLFAbstractPropertizedObject *pobj)
00284 {
00285   d->model->setPObj(pobj);
00286 }
00287 
00288 
00289 
00290 
00291 
00292 

Generated by doxygen 1.7.6.1. The KLatexFormula website is hosted on sourceforge.net