Subversion Repositories OpenInventory

Rev

Rev 167 | 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)
{
  //  SpinBoxDelegate::getItems();
}
//! [0]

//! [1]
QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
    const QStyleOptionViewItem &/* option */,
    const QModelIndex &/* index */) const
{
    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
{
    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);
    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
{
    QStringList ID_list;
    QStringList Name_list;

    QComboBox *comboBox = static_cast<QComboBox*>(editor);
    int currIndex;
    currIndex = comboBox->currentIndex();
  //  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]

void SpinBoxDelegate::getItems(){
    QMap <QString, QString> result_map;
    QSqlQuery q;
    QString query_str;
    bool ok;

    query_str = tr("select * from `ListOfClasses`");
    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 ClassName;
                  ID = q.value(0).toString(); // îïðåäåëÿåì ID
                  ClassName = q.value(1).toString(); // îïðåäåëÿåì Name
                  items.insert(ID, ClassName);
                }


 //   return result_map;
}

















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)
   {
   }





   QWidget *CPictureDelegate::createEditor(QWidget *parent,
       const QStyleOptionViewItem &/* option */,
       const QModelIndex &/* index */) const
   {

       IconForm *editor = new IconForm(parent);
       return editor;

   }




   void CPictureDelegate::setEditorData(QWidget *editor,
                                       const QModelIndex &index) const
   {


       const QAbstractItemModel * model = index.model();
       IconForm *icnFrm = static_cast<IconForm*>(editor);
       QVariant currentImage = model->data(index,0);
       QByteArray bytes = currentImage.toByteArray();
       if (currentImage.isValid()) {
            m_pxPicture.loadFromData(bytes);
       }
       else {
           //QItemDelegate::paint(painter, option, index);


           return;
       }
       icnFrm->setPixmap(m_pxPicture);
   }




   void CPictureDelegate::setModelData(
               QWidget *editor,
               QAbstractItemModel *model,
               const QModelIndex& index) const {
       IconForm *icnFrm = static_cast<IconForm*>(editor);
       if (!(icnFrm->dataIsChanged())) return;

       if (!(icnFrm->pixmap().isNull())){ // åñëè íå óäàëèëè èêîíêó
       m_pxPicture = icnFrm->pixmap();
       QImage currentImage = m_pxPicture.toImage();
          QByteArray bytes;
          QBuffer buffer(&bytes);
          buffer.open(QIODevice::WriteOnly);
          currentImage.save(&buffer, "PNG");




         model->setData(index, QVariant (bytes), Qt::EditRole);

     //    int widht = m_pxPicture.width();
     //    int heigh =  m_pxPicture.height();
     //    tblView->setRowHeight(index.row(), heigh + 10);
         // model->submitAll();
     }
       else { // èêîíêó óäàëèëè
           QVariant val_null;
           model->setData(index, val_null, Qt::EditRole);
       }


      // model->setData(index, value);
   }



   void CPictureDelegate::updateEditorGeometry(
               QWidget *editor,
               const QStyleOptionViewItem &option,
               const QModelIndex& /* index */) const {


   //    QRect r(option.rect.x() + option.rect.width()/2 - 7, option.rect.y() + option.rect.height()/2 - 7, 150, 100);
     //      editor->setGeometry(r);

        editor->setGeometry(option.rect);
   }




   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();

          QVariant currentImage = model->data(index,0);

           QByteArray bytes = currentImage.toByteArray();
     //       QImage image;
     //       image.loadFromData(bytes);


           if (currentImage.isValid()) {
                m_pxPicture.loadFromData(bytes);

           }
           else {
           int i;
           i++;
               //QItemDelegate::paint(painter, option, index);
           return;
           }





           /*

           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 CPictureDelegate::setTableView(QTableView* table){
       tblView = table;
   }