00001 #include <QSortFilterProxyModel>
00002 #include <QDebug>
00003 #include <QFile>
00004 #include <QFileInfo>
00005 #include <QFileDialog>
00006 #include "../Data/settings.h"
00007 #include "accountstabledialog.h"
00008 #include "../Views/accountstabledelegate.h"
00009
00010 AccountsTableDialog::AccountsTableDialog(const QString &path, Settings &settings, QWidget *parent) : QDialog(parent)
00011 {
00012 setupUi(this);
00013 createActions();
00014 setAttribute(Qt::WA_DeleteOnClose, true);
00015
00016 editing = false;
00017
00018 if(QFileInfo(path).exists())
00019 {
00020 pathToTableOfAccounts = path;
00021 loadTableOfAccounts();
00022 }
00023 else
00024 {
00025 pathToTableOfAccounts = path;
00026 newTableOfAccounts();
00027 }
00028 }
00029
00030 void AccountsTableDialog::createActions()
00031 {
00032 deleteAction = new QAction(this);
00033 deleteAction->setIcon(QIcon(":/project/DeleteDocument"));
00034 deleteAction->setText(tr("Löschen"));
00035
00036 editAction = new QAction(this);
00037 editAction->setIcon(QIcon(":/project/EditDocument"));
00038 editAction->setText(tr("Bearbeiten"));
00039
00040 connect(deleteAction, SIGNAL(triggered()), this, SLOT(removeEntry()));
00041 connect(editAction, SIGNAL(triggered()), this, SLOT(editEntry()));
00042
00043 connect(buttonAddEditAccount, SIGNAL(clicked()), this, SLOT(addEditAccount()));
00044 connect(buttonSaveTableOfAccounts, SIGNAL(clicked()), this, SLOT(saveTableOfAccounts()));
00045 connect(buttonClose, SIGNAL(clicked()), this, SLOT(close()));
00046 }
00047
00048 void AccountsTableDialog::close()
00049 {
00050 emit tableOfAccountsCreated(pathToTableOfAccounts);
00051 done(QDialog::Accepted);
00052 }
00053
00054 void AccountsTableDialog::loadTableOfAccounts()
00055 {
00056 initializeControlsAndSetupModel();
00057
00058 QFile file(pathToTableOfAccounts);
00059 QDomDocument doc("Allevo");
00060 if(!file.open(QIODevice::ReadOnly))
00061 {
00062 qDebug()<<"couldn't read table of accounts";
00063 return;
00064 }
00065 if(!doc.setContent(&file))
00066 {
00067 qDebug()<<"couldn't read table of accounts -- invalid syntax";
00068 file.close();
00069 return;
00070 }
00071 file.close();
00072 QDomElement root = doc.documentElement();
00073 QDomNode n = root.firstChild();
00074 QDomNode node = n.firstChild();
00075 while(!node.isNull())
00076 {
00077 if(node.isElement() && node.nodeName() == "account")
00078 {
00079 QDomElement e = node.toElement();
00080 model->addEntry(e.attribute("number"), e.attribute("name"), e.attribute("description"));
00081
00082 node = node.nextSibling();
00083 }
00084 }
00085
00086 tableAccounts->resizeRowsToContents();
00087 tableAccounts->resizeColumnsToContents();
00088 }
00089
00090 void AccountsTableDialog::newTableOfAccounts()
00091 {
00092 initializeControlsAndSetupModel();
00093
00094 saveTableOfAccounts();
00095 loadTableOfAccounts();
00096 }
00097
00098 void AccountsTableDialog::initializeControlsAndSetupModel()
00099 {
00100 labelDocumentName->setText(QFileInfo(pathToTableOfAccounts).baseName());
00101
00102 model = new AccountsTableModel(this);
00103 filterModel = new AccountsTableFilterModel(this);
00104 filterModel->setDynamicSortFilter(true);
00105 filterModel->setSourceModel(model);
00106
00107 tableAccounts->setModel(filterModel);
00108 tableAccounts->addAction(deleteAction);
00109 tableAccounts->addAction(editAction);
00110 tableAccounts->setContextMenuPolicy(Qt::ActionsContextMenu);
00111
00112 tableAccounts->setItemDelegate(new AccountsTableDelegate(tableAccounts));
00113
00114 tableAccounts->setSelectionBehavior(QAbstractItemView::SelectRows);
00115 connect(textFilter, SIGNAL(textChanged(QString)), filterModel, SLOT(setFilterFixedString(QString)));
00116
00117 textAccount->setHintText("Kontonummer");
00118 textName->setHintText("Kontoname");
00119 textDescription->setHintText("kurze Beschreibung");
00120 }
00121
00122 void AccountsTableDialog::saveTableOfAccounts()
00123 {
00124 QDomDocument xml;
00125 QDomElement root = xml.createElement(QCoreApplication::applicationName());
00126 root.setAttribute("program", QCoreApplication::organizationDomain());
00127 root.setAttribute("version", QCoreApplication::applicationVersion());
00128 xml.appendChild(root);
00129 root.appendChild(model->dataNode());
00130
00131 QFile file(pathToTableOfAccounts);
00132 if(!file.open(QIODevice::WriteOnly))
00133 {
00134 qDebug()<<"couldn't write table of accounts";
00135 return;
00136 }
00137 QTextStream ts(&file);
00138 ts << xml.toString();
00139 file.close();
00140 }
00141
00142 void AccountsTableDialog::removeEntry()
00143 {
00144 QTableView *temp = static_cast<QTableView*>(tableAccounts);
00145 QItemSelectionModel *selectionModel = temp->selectionModel();
00146 QItemSelection selection = ((QSortFilterProxyModel*)(temp->model()))->mapSelectionToSource(selectionModel->selection());
00147
00148 qDebug()<<"selected row: " << selection.indexes().value(0).row();
00149 model->removeRows(selection.indexes().value(0).row(), 1, QModelIndex());
00150 }
00151
00152 void AccountsTableDialog::editEntry()
00153 {
00154 switchInputModeToEditing();
00155 }
00156
00157 void AccountsTableDialog::addEditAccount()
00158 {
00159
00160 bool completed = true;
00161
00162 QRegExp ex("^[0-9]{4,5}$");
00163 if(ex.exactMatch(textAccount->text()))
00164 {
00165 if(!(textAccount->text().startsWith("2") || textAccount->text().startsWith("3")) && textAccount->text().length() == 5)
00166 {
00167 QMessageBox::warning(this, "Keine gültige Kontonummer", "Nur die 2er und 3er Klasse können eine 5-stellige Kontonummer haben", QMessageBox::Ok);
00168 }
00169 else
00170 {
00171 if(textName->text() != "Kontoname" || textName->text() == "")
00172 {
00173 if(!editing)
00174 {
00175 addAccount();
00176 }
00177 else
00178 {
00179 editAccount();
00180 }
00181 }
00182 else
00183 {
00184 QMessageBox::warning(this, "Fehler", "Bitte geben Sie einen Kontonamen ein!", QMessageBox::Ok);
00185 completed = false;
00186 }
00187 }
00188 }
00189 else
00190 {
00191 QMessageBox::warning(this, "Fehler", "Die Kontonummer ist ungültig! Bitte überprüfen Sie Ihre Eingabe!", QMessageBox::Ok);
00192 completed = false;
00193 }
00194
00195 if(completed)
00196 {
00197 clearControls();
00198 switchInputModeToAdding();
00199 }
00200 }
00201
00202 void AccountsTableDialog::addAccount()
00203 {
00204 if(textDescription->text() == textDescription->hintText())
00205 {
00206 if(!model->addEntry(textAccount->text(), textName->text(), ""))
00207 {
00208 QMessageBox::warning(this, "Fehler", "Dieses Konto existiert bereits! Bitte geben Sie ein anderes Konto ein!", QMessageBox::Ok);
00209 }
00210 }
00211 else
00212 {
00213 if(!model->addEntry(textAccount->text(), textName->text(), textDescription->text()))
00214 {
00215 QMessageBox::warning(this, "Fehler", "Dieses Konto existiert bereits! Bitte geben Sie ein anderes Konto ein!", QMessageBox::Ok);
00216 }
00217 }
00218 }
00219
00220 void AccountsTableDialog::editAccount()
00221 {
00222 QModelIndexList list = tableAccounts->selectionModel()->selectedIndexes();
00223 model->setData(list.value(0), textAccount->text(), Qt::EditRole);
00224 model->setData(list.value(1), textName->text(), Qt::EditRole);
00225 if(textDescription->text() == textDescription->hintText())
00226 {
00227 model->setData(list.value(2), "", Qt::EditRole);
00228 }
00229 else
00230 {
00231 model->setData(list.value(2), textDescription->text(), Qt::EditRole);
00232 }
00233 }
00234
00235 void AccountsTableDialog::switchInputModeToAdding()
00236 {
00237 buttonAddEditAccount->setText("Konto hinzufügen");
00238 buttonSaveTableOfAccounts->setEnabled(true);
00239 buttonClose->setEnabled(true);
00240 textFilter->setEnabled(true);
00241 tableAccounts->setEnabled(true);
00242
00243 editing = false;
00244 }
00245
00246 void AccountsTableDialog::switchInputModeToEditing()
00247 {
00248 tableAccounts->setEnabled(false);
00249 textFilter->setEnabled(false);
00250 buttonSaveTableOfAccounts->setEnabled(false);
00251 buttonClose->setEnabled(false);
00252
00253 QModelIndexList list = tableAccounts->selectionModel()->selectedIndexes();
00254
00255 textAccount->setText(static_cast<QModelIndex>(list.value(0)).data(Qt::DisplayRole).toString());
00256 textName->setText(list.value(1).data(Qt::DisplayRole).toString());
00257 textDescription->setText(static_cast<QModelIndex>(list.value(2)).data(Qt::DisplayRole).toString());
00258
00259 buttonAddEditAccount->setText("Konto bearbeiten");
00260 editing = true;
00261 }
00262
00263 void AccountsTableDialog::clearControls()
00264 {
00265 textAccount->setText("");
00266 textAccount->showHintText();;
00267
00268 textName->setText("");
00269 textName->showHintText();
00270
00271 textDescription->setText("");
00272 textDescription->showHintText();
00273 }