Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Yingdi Yu |
| 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 9 | */ |
| 10 | |
| 11 | #include "profileeditor.h" |
| 12 | #include "ui_profileeditor.h" |
| 13 | #include <QtSql/QSqlRecord> |
| 14 | #include <QtSql/QSqlField> |
| 15 | #include <QtSql/QSqlError> |
| 16 | |
| 17 | #ifndef Q_MOC_RUN |
| 18 | #include "logging.h" |
| 19 | #include "exception.h" |
| 20 | #endif |
| 21 | |
| 22 | using namespace ndn; |
| 23 | |
| 24 | INIT_LOGGER("ProfileEditor"); |
| 25 | |
| 26 | ProfileEditor::ProfileEditor(Ptr<ContactStorage> contactStorage, QWidget *parent) |
| 27 | : QDialog(parent) |
| 28 | , ui(new Ui::ProfileEditor) |
| 29 | , m_tableModel(new QSqlTableModel()) |
| 30 | , m_contactStorage(contactStorage) |
| 31 | { |
| 32 | ui->setupUi(this); |
| 33 | |
| 34 | connect(ui->addRowButton, SIGNAL(clicked()), |
| 35 | this, SLOT(onAddClicked())); |
| 36 | connect(ui->deleteRowButton, SIGNAL(clicked()), |
| 37 | this, SLOT(onDeleteClicked())); |
| 38 | connect(ui->okButton, SIGNAL(clicked()), |
| 39 | this, SLOT(onOkClicked())); |
| 40 | |
| 41 | m_tableModel->setEditStrategy(QSqlTableModel::OnManualSubmit); |
| 42 | m_tableModel->setTable("SelfProfile"); |
| 43 | m_tableModel->select(); |
| 44 | m_tableModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Index")); |
| 45 | m_tableModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Type")); |
| 46 | m_tableModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Value")); |
| 47 | |
| 48 | ui->profileTable->setModel(m_tableModel); |
| 49 | ui->profileTable->setColumnHidden(0, true); |
| 50 | ui->profileTable->show(); |
| 51 | |
| 52 | } |
| 53 | |
| 54 | ProfileEditor::~ProfileEditor() |
| 55 | { |
| 56 | delete ui; |
| 57 | delete m_tableModel; |
| 58 | } |
| 59 | |
| 60 | void |
| 61 | ProfileEditor::onAddClicked() |
| 62 | { |
| 63 | int rowCount = m_tableModel->rowCount(); |
| 64 | |
| 65 | // QSqlRecord record; |
| 66 | // QSqlField typeField("profile_type", QVariant::String); |
| 67 | // QSqlField valueField("profile_value", QVariant::String); |
| 68 | // record.append(typeField); |
| 69 | // record.append(valueField); |
| 70 | // record.setValue("profile_type", QString("N/A")); |
| 71 | // record.setValue("profile_value", QString("N/A")); |
| 72 | |
| 73 | // bool res = m_tableModel->insertRecord(-1, record); |
| 74 | |
| 75 | // res = m_tableModel->submitAll(); |
| 76 | m_tableModel->insertRow(rowCount); |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | ProfileEditor::onDeleteClicked() |
| 81 | { |
| 82 | QItemSelectionModel* selectionModel = ui->profileTable->selectionModel(); |
| 83 | QModelIndexList indexList = selectionModel->selectedRows(); |
| 84 | |
| 85 | int i = indexList.size() - 1; |
| 86 | for(; i >= 0; i--) |
| 87 | { |
| 88 | if(0 != indexList[i].row()) |
| 89 | m_tableModel->removeRow(indexList[i].row()); |
| 90 | } |
| 91 | m_tableModel->submitAll(); |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | ProfileEditor::onOkClicked() |
| 96 | { |
| 97 | m_tableModel->submitAll(); |
| 98 | this->hide(); |
| 99 | } |
| 100 | |
| 101 | #if WAF |
| 102 | #include "profileeditor.moc" |
| 103 | #include "profileeditor.cpp.moc" |
| 104 | #endif |