Хранилища Subversion OpenInventory

Редакция

Редакция 226 | Авторство | Сравнить с предыдущей | Последнее изменение | Открыть журнал | Скачать | RSS



/****************************************************************************
**
** 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);
//    editor->setEditable(false);
    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(false);


//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);
}











//! [0]
FilterSpinBoxDelegate::FilterSpinBoxDelegate(QObject *parent)
    : QItemDelegate(parent)
{
}
//! [0]

void FilterSpinBoxDelegate::setClassID(QString newClassID){
    class_id = newClassID;
}

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

    if (FilterSpinBoxDelegate::class_id.isEmpty())
    {
        items.clear();
        return;
    }

    query_str = tr("select * from `DescriptionOfClasses` where `ClassIdentifer` = '");
    query_str.append(class_id);
    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 *FilterSpinBoxDelegate::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);
//    editor->setEditable(false);
    return editor;
}
//! [1]

//! [2]
void FilterSpinBoxDelegate::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(false);


//comboBox->setItemText(0, value);

}
//! [2]

//! [3]
void FilterSpinBoxDelegate::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);
    QString value = Name_list.at(currIndex);
    model->setData(index, value, Qt::EditRole);
}
//! [3]

//! [4]
void FilterSpinBoxDelegate::updateEditorGeometry(QWidget *editor,
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
    editor->setGeometry(option.rect);
}










//! [0]
FilterConditionDelegate::FilterConditionDelegate(QObject *parent)
    : QItemDelegate(parent)
{
}
//! [0]

void FilterConditionDelegate::setClassID(QString newClassID){
    class_id = newClassID;
}



//! [1]
QWidget *FilterConditionDelegate::createEditor(QWidget *parent,
    const QStyleOptionViewItem &/* option */,
    const QModelIndex &/* index */) const
{
    QStringList Name_list;

    Name_list = items;

    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);
//    editor->setEditable(false);
    return editor;
}
//! [1]

//! [2]
void FilterConditionDelegate::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 = items.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(false);


//comboBox->setItemText(0, value);

}
//! [2]

//! [3]
void FilterConditionDelegate::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);
    QString value = items.at(currIndex);
    model->setData(index, value, Qt::EditRole);
}
//! [3]

//! [4]
void FilterConditionDelegate::updateEditorGeometry(QWidget *editor,
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
    editor->setGeometry(option.rect);
}










FilterValueDelegate::FilterValueDelegate(QObject *parent)
    : QItemDelegate(parent)
{
}
//! [0]

void FilterValueDelegate::setClassID(QString newClassID){
    class_id = newClassID;
}



// ïîëó÷àåì èìÿ òàáëèöû - õðàíèëèùà îáúåêòîâ êëàññà
QString FilterValueDelegate::getClassInctanc(QString class_id){




    QSqlQuery q;
    QString sql_str;
    QString result;
    QString classInctance;
    int field_inctance;
    bool ok;
    sql_str = tr("select * "
                 " from ListOfClasses where  ListOfClasses.ID = '"       //îïðåäåëèì êëàññû, äëÿ êîòîðûõ êîðíåâîé ÿâëÿåòñÿ êîíòåéíåðîì
                 );
    sql_str.append(class_id);
     sql_str.append(tr("'"));
    q.prepare(sql_str);

    ok = q.exec();

    if (!ok) return result;

    /*
    if (!ok) {
                            QMessageBox::critical( // Äèàëîã ñ ñîîáùåíèåì îá îøèáêå.
                                                                            this,                      // Ðîäèòåëüñêèé âèäæåò.
                                                                            QObject::tr("Database Error"),   // Çàãîëîâîê.
                                                                            q.lastError().text());          // Òåêñò ñîîáùåíèÿ.
                                                                            return result;
                     }

   */





    field_inctance = q.record().indexOf(tr("TableWhithInstance"));
    while(q.next()){

                  classInctance = q.value(field_inctance).toString();
                  result.append(classInctance);
                  }

  return result;





}



 QString FilterValueDelegate::getFieldType(QString class_id, QString fieldName)//îïðåäåëÿåì òèï äàííûõ ïîëÿ
 {

 }

 QMap<QString, QString>  FilterValueDelegate::getItems(QString inctance){ // âîçâðàùàåì ñïèñîê âñåõ çíà÷åíèé èç òàáëèöû-õðàíèëèùà
    QSqlQuery q;
    QString sql_str;
    QStringList keys_list;
    QStringList values_list;
    QMap<QString, QString> result_map;




    bool ok;
    sql_str = tr("select * from `");
    sql_str.append(inctance);
    sql_str.append(tr("`"));
    ok = q.prepare(sql_str);
    ok = q.exec();
    if(!ok) return result_map;
    while (q.next()){
        QString key_tmp;
        QString value_tmp;

         key_tmp = q.record().value(0).asString(); //îïðåäåëÿåì id îáúåêòà
         //keys_list.append(tmp_str); // äîáàâèì â ñïèñîê êëþ÷åé

        value_tmp = q.record().value(1).asString(); //îïðåäåëÿåì èìÿ îáúåêòà

        result_map.insert(key_tmp,  value_tmp);
        // values_list.append(tmp_str);


    }





    return result_map;

 }




//! [1]
QWidget *FilterValueDelegate::createEditor(QWidget *parent,
    const QStyleOptionViewItem &/* option */,
    const QModelIndex &/* index */) const
{
    QStringList Name_list;

    Name_list = items;

    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);
    editor->setEditable(true);
    return editor;
}
//! [1]




TField FilterValueDelegate::getField(const QModelIndex &index) // ïî èíäåêñó (â òàáëèöå ôèëüòðîâ) ïîëó÷èì èíôîðìàöèþ î ïîëå
{
    const QAbstractItemModel * model = index.model();

    TField field_result;
    QSqlQuery q;
    QString sql_str;
    bool ok;


    // QString fieldName = index.model()->data(model->index(index.row(),4), Qt::EditRole).toString();
  //  QString class_id = index.model()->data(model->index(index.row(),2), Qt::EditRole).toString();
    field_result.FieldName = index.model()->data(model->index(index.row(),4), Qt::EditRole).toString();
    field_result.ClassIdentifer = index.model()->data(model->index(index.row(),2), Qt::EditRole).toString();

    sql_str = tr("select * from `DescriptionOfClasses` where `ClassIdentifer` = '");
    sql_str.append(field_result.ClassIdentifer);
    sql_str.append(tr("' and `FieldName` = '"));
    sql_str.append(field_result.FieldName);
    sql_str.append(tr("'"));

    ok = q.prepare(sql_str);

    if(!ok)  return field_result;

    ok = q.exec();

    if(!ok)  return field_result;

    int ID_index = q.record().indexOf(tr("ID"));

    int FieldAlias_index = q.record().indexOf(tr("FieldAlias"));

    int FieldType_index = q.record().indexOf(tr("FieldType"));

    int DefaultValue_index = q.record().indexOf(tr("DefaultValue"));

    int Comment_index = q.record().indexOf(tr("Comment"));



    while(q.next()){
    field_result.ID = q.value(ID_index).asString();
    field_result.FieldAlias = q.value(FieldAlias_index).asString();
    field_result.FieldType = q.value(FieldType_index).asString();
    field_result.DefaultValue = q.value(DefaultValue_index).asString();
    field_result.Comment = q.value(Comment_index).asString();
    }

    return field_result;



}





//! [2]
void FilterValueDelegate::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);


const QAbstractItemModel * model = index.model();


QString value = index.model()->data(index, Qt::EditRole).toString();
QString class_id = index.model()->data(model->index(index.row(),2), Qt::EditRole).toString();
QComboBox *comboBox = static_cast<QComboBox*>(editor);
QString inctance; //õðàíèëèùå îáúåêòîâ êëàññà
QString fieldName;//èìÿ ïîëÿ
QSqlQuery q;
QString sql_str;
QStringList itemsList;
bool ok;
TField Field;

Field = getField(index);


if (Field.FieldType == tr("pointer")){ // åñëè äàííîå ïîëå õðàíèò óêàçàòåëè íà êëàññ-êîíòåéíåð
    QString parent_inctance;
    QMap<QString, QString> items_map;
    QStringList items_list;

    parent_inctance = getClassInctanc(Field.DefaultValue);// îïðåäåëÿåì õðàíèëèùå îáúåêòîâ êëàññà-êîíòåéíåðà
    items_map = getItems(parent_inctance);
    items_list = items_map.values();

    comboBox->addItems(items_list);
    if (items_list.indexOf(value)!=-1) comboBox->setCurrentIndex(items_list.indexOf(value));
    return;



}



fieldName = index.model()->data(model->index(index.row(),4), Qt::EditRole).toString();


inctance = FilterValueDelegate::getClassInctanc(class_id);
if (!inctance.isEmpty() && !fieldName.isEmpty()){
sql_str = tr("select `");
sql_str.append(fieldName);
sql_str.append(tr("` from `"));
sql_str.append(inctance);
sql_str.append(tr("`"));
ok = q.prepare(sql_str);
ok = q.exec();

while(q.next()){
QString tmp_str;
tmp_str = q.record().value(fieldName).asString();
itemsList.append(tmp_str);
FilterValueDelegate::items.append(tmp_str);
        }
if(!itemsList.isEmpty()) {
                            comboBox->addItems(itemsList);
                            items = itemsList; // ãëîáàëüãàÿ ïåðåìåííàÿ
                         }

}





//QStringList ID_list;
//QStringList Name_list;
int curr_index;

//ID_list = items.keys();
//Name_list = items.values();

curr_index = itemsList.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 FilterValueDelegate::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);

    QComboBox *comboBox = static_cast<QComboBox*>(editor);
     int currIndex;
    currIndex = comboBox->currentIndex();

    if (currIndex==-1) return;

    QString new_value = comboBox->itemText(currIndex);

    QString curr_value = model->data(index, Qt::EditRole).toString();//òåêóùåå çíà÷åíèå â ìîäåëè



    QString class_id = model->data(model->index(index.row(),2), Qt::EditRole).toString();//îïðåäåëèì êëàññ, ê êîòîðîìó ïðåíàäëåæèò ïîëå


    QStringList ID_list;
    QStringList Name_list;


    TField Field;

    Field = getField(index);

    if(Field.FieldType==tr("pointer")){
        QString parent_inctance;
        QMap<QString, QString> items_map;
        QStringList keys_list; // ñïèñîê ID îáúåêòîâ ðîäèòåëüñêîãî êëàññà
        QStringList values_list; //ñïèñîê èìåí îáúåêòîâ ðîäèòåëüñêîãî êëàññà



        parent_inctance = getClassInctanc(Field.DefaultValue);// îïðåäåëÿåì õðàíèëèùå îáúåêòîâ êëàññà-êîíòåéíåðà
        items_map = getItems(parent_inctance); //
        keys_list = items_map.keys();
        values_list = items_map.values();

        if( values_list.indexOf(new_value)!=-1){ //

            QString new_key =  keys_list.at(values_list.indexOf(new_value)); // îïðåäåëÿåì íîâîå ØÂ
            if (new_key != curr_value) model->setData(index, new_key, Qt::EditRole);
            return;
        }




    }







 //   QString new_value = comboBox->itemText(currIndex);




    // ID_list = items.keys();
    // Name_list = items.values();

 //   QString value = ID_list.at(currIndex);
  //  QString value = items.at(currIndex);
     model->setData(index, new_value, Qt::EditRole);


}








//! [3]

//! [4]
void FilterValueDelegate::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();
   }






   IconDelegate::IconDelegate( QObject * parent ) : QItemDelegate(parent)
   {
   }





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

       IconForm *editor = new IconForm(parent);

       return editor;

   }




   void IconDelegate::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);
     //  tblView->setRowHeight(index.row(), icnFrm->geometry().height());
   }




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

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




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



   void IconDelegate::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 IconDelegate::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 {
               //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->setRenderHint(QPainter::Antialiasing);
           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 IconDelegate::setTableView(QTableView* table){
       tblView = table;
   }








   FileDelegate::FileDelegate( QObject * parent ) : QItemDelegate(parent)
   {
   }





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

       FileForm *editor = new FileForm(parent);

       return editor;

   }




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


       const QAbstractItemModel * model = index.model();
       FileForm *flFrm = static_cast<FileForm*>(editor);
       QVariant currentData = model->data(index,Qt::EditRole);

      // QVariant currentData = QSqlTableModel::data(index, 0);
       QByteArray bytes = currentData.toByteArray();
       if (currentData.isValid()) flFrm->setData(bytes);
       else return;

   }




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

       m_Data = flFrm->data();
       if (!m_Data.isEmpty()) { // åñëè ìàññèâ íå ïóñòîé
                                   model->setData(index, QVariant (m_Data), Qt::EditRole);

                              }
       else {       // åñëè ìàññèâ ïóñòîé (ôàéë óäàëèëè)
           QVariant val_null;
           model->setData(index, val_null, Qt::EditRole);

            }



   }



   void FileDelegate::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);
   }