Rev 138 |
Rev 169 |
Go to most recent revision |
Blame |
Compare with Previous |
Last modification |
View Log
| Download
| RSS feed
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/
/*
delegate.cpp
A delegate that allows the user to change integer values from the model
using a spin box widget.
*/
#include <QtGui>
#include "delegate.h"
//! [0]
SpinBoxDelegate::SpinBoxDelegate(QObject *parent)
: QItemDelegate(parent)
{
}
//! [0]
void SpinBoxDelegate::setInctance(QString newInctance){
inctance = newInctance;
}
void SpinBoxDelegate::getItems(){
QMap <QString, QString> result_map;
QSqlQuery q;
QString query_str;
bool ok;
if (SpinBoxDelegate::inctance.isEmpty())
{
items.clear();
return;
}
query_str = tr("select * from `");
query_str.append(inctance);
query_str.append( tr("`"));
q.prepare(query_str);
ok = q.exec();
if (!ok) {
/*
QString error_str;
error_str = tr("íå óäàëîñü ïîëó÷èòü ñïèñîê îáúåêòîâ èç òàáëèöû ");
error_str.append(inctance);
QMessageBox::critical( // Äèàëîã ñ ñîîáùåíèåì îá îøèáêå.
this, // Ðîäèòåëüñêèé âèäæåò.
QObject::tr("Database Error"), // Çàãîëîâîê.
q.lastError().text()); // Òåêñò ñîîáùåíèÿ.
// tr("íå óäàëîñü ïîëó÷èòü ñïèñîê îáúåêòîâ èç òàáëèöû ")); // Òåêñò ñîîáùåíèÿ.
*/
items.clear();
return;
}
// field_inctance = q.record().indexOf(tr("TableWhithInstance"));
items.clear();
while(q.next()){
QString ID;
QString Name;
ID = q.value(0).toString(); // îïðåäåëÿåì ID
Name = q.value(1).toString(); // îïðåäåëÿåì Name
items.insert(ID, Name);
}
// return result_map;
}
//! [1]
QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */) const
{
//QSpinBox *editor = new QSpinBox(parent);
//editor->setMinimum(0);
//editor->setMaximum(100);
QStringList ID_list;
QStringList Name_list;
// getItems();
ID_list = items.keys();
Name_list = items.values();
QComboBox *editor = new QComboBox(parent);
/*
editor->addItem("0");
editor->addItem("1");
editor->addItem("2");
editor->addItem("3");
editor->addItem("4");
editor->addItem("5");
editor->addItem("6");
editor->addItem("7");
editor->addItem("8");
editor->addItem("9");
*/
// editor->addItem("0");
editor->addItems(Name_list);
return editor;
}
//! [1]
//! [2]
void SpinBoxDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
// int value = index.model()->data(index, Qt::EditRole).toInt();
// QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
// spinBox->setValue(value);
QString value = index.model()->data(index, Qt::EditRole).toString();
QComboBox *comboBox = static_cast<QComboBox*>(editor);
QStringList ID_list;
QStringList Name_list;
int curr_index;
ID_list = items.keys();
Name_list = items.values();
curr_index = ID_list.indexOf(value);
if (curr_index==-1) return;
comboBox->setCurrentIndex(curr_index);
/****************************************************
if (value == "0") comboBox->setCurrentIndex(0);
if (value == "1") comboBox->setCurrentIndex(1);
if (value == "2") comboBox->setCurrentIndex(2);
if (value == "3") comboBox->setCurrentIndex(3);
if (value == "4") comboBox->setCurrentIndex(4);
if (value == "5") comboBox->setCurrentIndex(5);
if (value == "6") comboBox->setCurrentIndex(6);
if (value == "7") comboBox->setCurrentIndex(7);
if (value == "8") comboBox->setCurrentIndex(8);
if (value == "9") comboBox->setCurrentIndex(9);
******************************************************/
comboBox->setEditable(true);
//comboBox->setItemText(0, value);
}
//! [2]
//! [3]
void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
// QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
// spinBox->interpretText();
// int value = spinBox->value();
// model->setData(index, value, Qt::EditRole);
QStringList ID_list;
QStringList Name_list;
QComboBox *comboBox = static_cast<QComboBox*>(editor);
int currIndex;
currIndex = comboBox->currentIndex();
if (currIndex==-1) return;
// QString value = comboBox->itemText(currIndex);
ID_list = items.keys();
Name_list = items.values();
QString value = ID_list.at(currIndex);
model->setData(index, value, Qt::EditRole);
}
//! [3]
//! [4]
void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}
//! [4]
MyDEDelegate::MyDEDelegate(
bool calpopup,
QObject *parent)
: QItemDelegate(parent),
m_calpopup(calpopup) {
}
QWidget *MyDEDelegate::createEditor(
QWidget *parent,
const QStyleOptionViewItem& /* option */,
const QModelIndex& /* index */) const {
QDateEdit *editor = new QDateEdit(parent);
editor->setCalendarPopup(m_calpopup);
editor->installEventFilter(const_cast<MyDEDelegate*>(this));
return editor;
}
void MyDEDelegate::setEditorData(
QWidget *editor,
const QModelIndex &index) const {
QDate value = index.model()->data(
index, Qt::EditRole).toDate();
QDateEdit *de = static_cast<QDateEdit*>(editor);
de->setDate(value);
}
void MyDEDelegate::setModelData(
QWidget *editor,
QAbstractItemModel *model,
const QModelIndex& index) const {
QDateEdit *de = static_cast<QDateEdit*>(editor);
de->interpretText();
QDate value = de->date();
model->setData(index, value);
}
void MyDEDelegate::updateEditorGeometry(
QWidget *editor,
const QStyleOptionViewItem &option,
const QModelIndex& /* index */) const {
editor->setGeometry(option.rect);
}
CPictureDelegate::CPictureDelegate( QObject * parent ) : QItemDelegate(parent)
{
}
void CPictureDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
m_pxPicture.fill( QColor(Qt::white) );
const QAbstractItemModel * model = index.model();
QString sFileName = model->data( index, Qt::DisplayRole ).toString();
if ( !sFileName.isEmpty() )
m_pxPicture.load( sFileName );
else {
//QItemDelegate::paint(painter, option, index);
return;
}
QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ?
((option.state & QStyle::State_Active) ? QPalette::Normal : QPalette::Inactive ) :
QPalette::Disabled;
if (option.state & QStyle::State_Selected)
painter->fillRect(option.rect, option.palette.color(cg, QPalette::Highlight));
int nX = option.rect.x() + ( ( option.rect.width() - m_pxPicture.rect().width() ) / 2 );
int nY = option.rect.y() + ( ( option.rect.height() - m_pxPicture.rect().height() ) / 2 );
painter->drawPixmap( nX, nY, m_pxPicture );
//// drawFocus(painter, option, option.rect.adjusted(0, 0, -1, -1)); // since we draw the grid ourselves
/*
QPen pen = painter->pen();
painter->setPen(option.palette.color(QPalette::Mid));
painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
painter->setPen(pen);
*/
}
void TimeEditDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
int datetime = index.model()->data(index, Qt::DisplayRole).toInt();
QString indexvalue = "";
if (datetime > 0)
{
QDateTime dateTime2 = QDateTime();
dateTime2.setTime_t(datetime);
indexvalue = dateTime2.toString(this->timeformat);
}
else
{
indexvalue = tr("Date not set");
}
Q_ASSERT(index.isValid());
QStyleOptionViewItemV3 opt = setOptions(index, option);
const QStyleOptionViewItemV2 *v2 = qstyleoption_cast<const QStyleOptionViewItemV2 *>(&option);
opt.features = v2 ? v2->features
: QStyleOptionViewItemV2::ViewItemFeatures(QStyleOptionViewItemV2::None);
const QStyleOptionViewItemV3 *v3 = qstyleoption_cast<const QStyleOptionViewItemV3 *>(&option);
opt.locale = v3 ? v3->locale : QLocale();
opt.widget = v3 ? v3->widget : 0;
// prepare
painter->save();
painter->setClipRect(opt.rect);
// get the data and the rectangles
QVariant value;
QPixmap pixmap;
QRect decorationRect;
value = index.data(Qt::DecorationRole);
QString text;
QRect displayRect;
value = index.data(Qt::DisplayRole);
if (value.isValid()) {
text = indexvalue;
displayRect = textRectangle(painter, option.rect, opt.font, text);
}
QRect checkRect;
Qt::CheckState checkState = Qt::Unchecked;
value = index.data(Qt::CheckStateRole);
if (value.isValid()) {
checkState = static_cast<Qt::CheckState>(value.toInt());
checkRect = check(opt, opt.rect, value);
}
// do the layout
doLayout(opt, &checkRect, &decorationRect, &displayRect, false);
// draw the item
drawBackground(painter, opt, index);
drawCheck(painter, opt, checkRect, checkState);
drawDecoration(painter, opt, decorationRect, pixmap);
drawDisplay(painter, opt, displayRect, text);
drawFocus(painter, opt, displayRect);
// done
painter->restore();
}