Редакция 79 | К новейшей редакции | Авторство | Сравнить с предыдущей | Последнее изменение | Открыть журнал | Скачать | RSS
#include "modelfordescriptiontable.h"
ModelForDescriptionTable::ModelForDescriptionTable(QObject *parent) :
QSqlTableModel(parent)
{
setEditStrategy(QSqlTableModel::OnManualSubmit);
}
Qt::ItemFlags ModelForDescriptionTable::flags(
const QModelIndex &index) const {
Qt::ItemFlags flags = QSqlTableModel::flags(index);
if ((index.column() != 5) && (index.column() != 0))
flags |= Qt::ItemIsEditable;
//if (index.column() == 4)
// flags |= Qt::ItemIsUserCheckable;
else
{
if ((index.column() == 0) || (index.column() == 5)) flags &= ~Qt::ItemIsEditable; // çàïðåùàåì ðåäàêòèðîâàíèå òðåòüåãî ñòîëáöà
}
return flags;
}
bool ModelForDescriptionTable::setData(const QModelIndex &index, const QVariant &value, int role) {
//// if (index.column()==3) return false; // â òðåòüåé êîëîíêå íå äàåì ïîìåíÿòü çíà÷åíèå
bool lResult = false;
QVariant old_value; // ñîõðàíèì ñòàðîå çíà÷åíèå
old_value = data(index);
/*
QString str_tmp;
if (index.column()==1) { // åñëè ìåíÿåì èìÿ êëàññà
str_tmp = value.toString(); // òî ïîìåíÿåì è èìÿ òàáëèöû
str_tmp.append(tr(" instance"));
}
*/
/*
QString str_tmp;
if (index.column()==0) { // åñëè ìåíÿåì èìÿ êëàññà
str_tmp = value.toString(); // òî ïîìåíÿåì è èìÿ òàáëèöû
str_tmp.append(tr("_instance"));
}
*/
QModelIndex newindex;
if (index.column()==1){ // åñëè ïîìåíÿëîñü èìÿ ïîëÿ
QSqlQuery qu;
QString old_field_name; // ñòàðîå èìÿ ïîëÿ
QString new_field_name; // íîâîå èìÿ ïîëÿ
QString query_str;
QString type_str; // òèï äàííûõ ïîëÿ
old_field_name = old_value.toString(); // îïðåäåëèì òåêóùåå (êîòîðîå íóæíî ïîìåíÿòü) èìÿ ïîëÿ
new_field_name = value.toString();
type_str = data(ModelForDescriptionTable::index(index.row(), 3)).toString(); // òèï ïîëÿ õðàíèòüñÿ â 3 ñòîëáöå, ñ÷èòûâàåì åãî çíà÷åíèå
query_str = tr("ALTER TABLE ");
query_str.append(ModelForDescriptionTable::Inctance);
query_str.append(tr(" CHANGE COLUMN `"));
query_str.append(old_field_name);
query_str.append(tr("` `"));
query_str.append(new_field_name);
query_str.append(tr("` "));
query_str.append(type_str);
query_str.append(tr(" CHARACTER SET cp1251 COLLATE cp1251_general_ci DEFAULT NULL"));
qu.prepare(query_str);
bool ok;
ok = qu.exec();
if (!ok) return false;
}
/*
if (!ok) {
QMessageBox::critical( // Äèàëîã ñ ñîîáùåíèåì îá îøèáêå.
this, // Ðîäèòåëüñêèé âèäæåò.
QObject::tr("Database Error"), // Çàãîëîâîê.
qu.lastError().text() ); // Òåêñò ñîîáùåíèÿ.
return ok; // âûõîäèì íå ïðèìåíÿÿ èçìåíåíèé
}
*/
// rec.setValue(index.column()+2, QVariant(str_tmp));
// rec.setGenerated(index.column()+2, true);
// newindex = MyModel::index(index.row(), index.column()+2);
// QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
/*
if (index.column()==0){
rec.setValue(index.column()+3, QVariant(str_tmp));
rec.setGenerated(index.column()+3, true);
newindex = MyModel::index(index.row(), index.column()+3);
QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
}
*/
if (index.column()==3){ // åñëè ïîìåíÿëñÿ òèï äàííûõ
///ALTER TABLE `an_db`.`1_Inctance` MODIFY COLUMN `int` INTEGER;
bool ok;
///// ok = setData(ModelForDescriptionTable::index(index.row(), 4), QVariant(tr("NULL")), Qt::EditRole); // ñáðîñèì çíà÷åíèå ïî óìîë÷àíèþ
ok = QSqlTableModel::setData(ModelForDescriptionTable::index(index.row(), 4), QVariant(tr("NULL")), Qt::EditRole); // ñáðîñèì çíà÷åíèå ïî óìîë÷àíèþ
if (ok) {
QSqlQuery qu;
QString old_type, new_type, field_name, query_str;
old_type = old_value.toString(); // îïðåäåëèì òåêóùèé (êîòîðîå íóæíî ïîìåíÿòü) òèï ïîëÿ
new_type = value.toString(); // íîâûé òèï ïîëÿ
field_name = data(ModelForDescriptionTable::index(index.row(), 1)).toString(); // èìÿ ïîëÿ õðàíèòüñÿ â 1 ñòîëáöå, ñ÷èòûâàåì åãî çíà÷åíèå
query_str = tr("ALTER TABLE ");
query_str.append(ModelForDescriptionTable::Inctance);
query_str.append(tr(" MODIFY COLUMN `"));
query_str.append(field_name);
query_str.append(tr("` "));
query_str.append(new_type);
qu.prepare(query_str);
ok = qu.exec();
if (!ok) return false;
}
else return false;
}
if (index.column()==4){ // åñëè ïîìåíÿëîñü çíà÷åíèå ïî óìîë÷àíèþ
///ALTER TABLE `an_db`.`2_Inctance` MODIFY COLUMN `Îò÷åñòâî` CHAR(10) CHARACTER SET cp1251 COLLATE cp1251_general_ci DEFAULT 'Èâàíîâè÷';
QSqlQuery qu;
QString field_type, new_default_value, field_name, query_str;
field_type = data(ModelForDescriptionTable::index(index.row(), 3)).toString(); // òèï ïîëÿ õðàíèòüñÿ â 3 ñòîëáöå, ñ÷èòûâàåì åãî çíà÷åíèå
field_name = data(ModelForDescriptionTable::index(index.row(), 1)).toString(); // èìÿ ïîëÿ õðàíèòüñÿ â 1 ñòîëáöå, ñ÷èòûâàåì åãî çíà÷åíèå
new_default_value = value.toString();
query_str = tr("ALTER TABLE ");
query_str.append(ModelForDescriptionTable::Inctance);
query_str.append(tr(" MODIFY COLUMN `"));
query_str.append(field_name);
query_str.append(tr("` "));
query_str.append(field_type);
if (new_default_value != tr("NULL")) {
query_str.append(tr(" DEFAULT '"));
query_str.append(new_default_value);
query_str.append(tr("'")); }
else {
query_str.append(tr(" DEFAULT ")); // Åñëè íàäî çàäàòü çíà÷åíèå NULL òî àïîñòðîôû íå ñòàâèì
query_str.append(new_default_value);
}
qu.prepare(query_str);
bool ok;
ok = qu.exec();
if (!ok) return false;
}
QSqlRecord rec = record(index.row());
rec.setValue(index.column(), value);
rec.setGenerated(index.column(), true);
lResult = QSqlTableModel::setData(index, value, role);
if (lResult) {
lResult = updateRowInTable(index.row(), rec);
emit field_changed();
/// QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
}
return lResult;
}
QVariant ModelForDescriptionTable::data(const QModelIndex &index, int role) const {
QVariant value = QSqlTableModel::data(index, role);
switch (role) {
case Qt::DisplayRole: return value;
case Qt::EditRole: return value;
case Qt::TextColorRole:
if(index.column() == 1)
return qVariantFromValue(QColor(Qt::darkGreen));
else
return value;
case Qt::TextAlignmentRole: return value;
case Qt::FontRole:
if(index.column() == 1) {
QFont font = QFont("Helvetica", 16, QFont::Bold);
return qVariantFromValue(font);
}else
return value;
case Qt::BackgroundColorRole: {
int a = (index.row() % 2) ? 14 : 0;
if(index.column() == 2)
return qVariantFromValue(QColor(220,240-a,230-a));
else if(index.column() == 6)
return qVariantFromValue(QColor(200,220-a,255-a));
else
return value;
}
case Qt::CheckStateRole: return value;
case Qt::SizeHintRole:return value;
}
return value;
}
void ModelForDescriptionTable::setInctance(QString currInctatce){ // ïåðåäàåì èìÿ òåêóùåé òàáëèöû-õðàíèëèùà
ModelForDescriptionTable::Inctance = currInctatce;
}