00001 #include "../Data/dataexercise.h"
00002 #include "../Data/dataaccount.h"
00003 #include "../Models/accountmodel.h"
00004 #include "../Widgets/alineedit.h"
00005 #include "../Calculations/dataexercisevalidator.h"
00006 #include <QLineEdit>
00007 #include <QHBoxLayout>
00008 #include <QComboBox>
00009 #include <QVBoxLayout>
00010 #include <QPushButton>
00011 #include <QList>
00012 #include <QPair>
00013 #include <QDebug>
00014 #include <QLabel>
00015
00016 #include "accountingrecordwidget.h"
00017 #include "../Widgets/acombobox.h"
00018 #include "../Widgets/alineedit.h"
00019 #include "../Data/project.h"
00020
00021 AccountingRecordWidget::AccountingRecordWidget(QWidget *parent) : QWidget(parent)
00022 {
00023 settings = NULL;
00024 validator = new DataExerciseValidator();
00025 connect(this, SIGNAL(beginToValidate(DataExercise*,AccountingInformation*,QList<AccountingInformation>*,QList<AccountingInformation>*)), validator, SLOT(exerciseFinished(DataExercise*, AccountingInformation*,QList<AccountingInformation>*,QList<AccountingInformation>*)));
00026 }
00027
00028 void AccountingRecordWidget::setSettings(Settings *settings)
00029 {
00030 this->settings = settings;
00031 }
00032
00033 void AccountingRecordWidget::makeButtonRow(int type)
00034 {
00035 if(type != 10)
00036 {
00037 QPushButton *buttonAddAccountingRecord = new QPushButton(QIcon(":/misc/Add"), tr("Buchungssatz hinzufügen"), this);
00038 QPushButton *buttonRemoveAccountingRecord = new QPushButton(QIcon(":/misc/Remove"), tr("Buchungssatz löschen"), this);
00039
00040 buttonRow->addSpacerItem(new QSpacerItem(60, 0));
00041 buttonRow->addWidget(buttonAddAccountingRecord);
00042 buttonRow->addWidget(buttonRemoveAccountingRecord);
00043
00044 if(type == 0)
00045 {
00046 QPushButton *buttonValidate = new QPushButton(QIcon(":/misc/Apply"), tr("Buchungssatz korrigieren"), this);
00047 QPushButton *buttonShowSolution = new QPushButton(QIcon(":misc/Apply"), tr("Lösung anzeigen"), this);
00048 buttonRow->addWidget(buttonValidate);
00049 buttonRow->addWidget(buttonShowSolution);
00050 connect(buttonValidate, SIGNAL(clicked()), this, SLOT(correctAccountingRecord()));
00051 connect(buttonShowSolution, SIGNAL(clicked()), this, SIGNAL(finishAccountingRecord()));
00052 }
00053
00054 buttonRow->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding));
00055 connect(buttonAddAccountingRecord, SIGNAL(clicked()), this, SLOT(addNewAccountingRecord()));
00056 connect(buttonRemoveAccountingRecord, SIGNAL(clicked()), this, SLOT(removeAccountingRecord()));
00057 }
00058 else
00059 {
00060 QPushButton *buttonBack = new QPushButton(QIcon(":/misc/Cancel"), tr("Zurück"), this);
00061
00062 buttonRow->addSpacerItem(new QSpacerItem(60, 0));
00063 buttonRow->addWidget(buttonBack);
00064 buttonRow->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding));
00065
00066 connect(buttonBack, SIGNAL(clicked(bool)), this, SIGNAL(backToAccountingRecord()));
00067 }
00068 }
00069
00070 void AccountingRecordWidget::clearSettings()
00071 {
00072 if(this->layout())
00073 {
00074 deleteWidget();
00075 delete layout();
00076 }
00077 p_layoutForAll = new QVBoxLayout();
00078 p_accountingRecordLayout = new QVBoxLayout();
00079 buttonRow = new QHBoxLayout();
00080 accountingList = new QList<DataAccountingRecord *>();
00081 }
00082
00083 void AccountingRecordWidget::createWidgetRow(bool editable, AccountModel *model, AccountingRecordWidgetType::type type, DataAccountingRecord *record)
00084 {
00085 AccountingRecord *rec = newAccountingRecord();
00086 connect(rec, SIGNAL(sendErrorMessage(QString)), this, SIGNAL(forwardErrorMesssage(QString)));
00087 connect(rec, SIGNAL(actualizeAccounts()), this, SLOT(saveBeforeActualizingAccounts()));
00088 p_accountingRecordList.append(rec);
00089 p_accountingRecordLayout->addWidget(rec);
00090
00091 if(type == AccountingRecordWidgetType::RowWithData)
00092 {
00093 rec->createWidgetForExercise(editable, model, showSolution, record);
00094 }
00095 else if(type == AccountingRecordWidgetType::RowWithOutData)
00096 {
00097 rec->createWidgetForExercise(true, model, showSolution);
00098 }
00099 }
00100
00101 void AccountingRecordWidget::createWidget(bool editable)
00102 {
00103 if(accountingList->size() == 0)
00104 {
00105
00106 createWidgetRow(editable, model, AccountingRecordWidgetType::RowWithOutData);
00107 }
00108 else
00109 {
00110
00111 for(int i = 0; i < accountingList->size(); i++)
00112 {
00113 createWidgetRow(editable, model, AccountingRecordWidgetType::RowWithData, accountingList->value(i));
00114 }
00115 }
00116
00117 p_layoutForAll->addLayout(p_accountingRecordLayout);
00118 p_layoutForAll->addLayout(buttonRow);
00119 this->setLayout(p_layoutForAll);
00120 }
00121
00122 void AccountingRecordWidget::makeWidgetForExercise(bool editable, AccountModel *model, DataExercise *exercise)
00123 {
00124 clearSettings();
00125 this->exercise = exercise;
00126 this->model = model;
00127 this->showSolution = false;
00128
00129 if(editable)
00130 {
00131 makeButtonRow(0);
00132 accountingList = exercise->accountingRecords();
00133 }
00134 else
00135 {
00136 makeButtonRow(10);
00137 accountingList = exercise->solutionRecords();
00138 }
00139
00140 createWidget(editable);
00141 }
00142
00143 AccountingRecord *AccountingRecordWidget::newAccountingRecord()
00144 {
00145 return new AccountingRecord(this, Project::dateCalendar());
00146 }
00147
00148 void AccountingRecordWidget::makeWidgetForTask(bool editable, AccountModel *model, DataExercise *exercise, bool showSolution)
00149 {
00150 clearSettings();
00151 this->exercise = exercise;
00152 this->model = model;
00153 this->showSolution = showSolution;
00154
00155 if(editable)
00156 {
00157 if(showSolution)
00158 {
00159 accountingList = exercise->solutionRecords();
00160 }
00161 else
00162 {
00163 accountingList = exercise->accountingRecords();
00164 }
00165 makeButtonRow(1);
00166 }
00167
00168 createWidget(editable);
00169 }
00170
00171 void AccountingRecordWidget::makeWidgetForPrinter(AccountModel *model, DataExercise *exercise, bool editable)
00172 {
00173 clearSettings();
00174 if(editable)
00175 {
00176 accountingList = exercise->accountingRecords();
00177 }
00178 else
00179 {
00180 accountingList = exercise->solutionRecords();
00181 }
00182 for(int i = 0; i < accountingList->size(); i++)
00183 {
00184 AccountingRecord *record = newAccountingRecord();
00185 record->createWidgetForPrinter(model, accountingList->value(i));
00186 p_accountingRecordList.append(record);
00187 p_accountingRecordLayout->addWidget(record);
00188 }
00189 p_layoutForAll->addLayout(p_accountingRecordLayout);
00190 this->setLayout(p_layoutForAll);
00191 }
00192
00193 void AccountingRecordWidget::removeAccountingRecord()
00194 {
00195 AccountingRecord *record = p_accountingRecordList.takeLast();
00196 p_accountingRecordLayout->removeWidget(record);
00197 record->deleteWidget();
00198 delete record;
00199 if(p_accountingRecordList.size() == 0)
00200 {
00201 AccountingRecord *rec = newAccountingRecord();
00202 rec->createWidgetForExercise(true, model);
00203 p_accountingRecordLayout->addWidget(record);
00204 p_accountingRecordList.append(record);
00205 }
00206 saveBeforeActualizingAccounts();
00207 }
00208
00209 void AccountingRecordWidget::addNewAccountingRecord()
00210 {
00211
00212 AccountingRecord *record = newAccountingRecord();
00213 connect(record, SIGNAL(sendErrorMessage(QString)), this, SIGNAL(forwardErrorMesssage(QString)));
00214 connect(record, SIGNAL(actualizeAccounts()), this, SLOT(saveBeforeActualizingAccounts()));
00215 if(settings->teacher())
00216 {
00217 record->createWidgetForExercise(true, model, true);
00218 }
00219 else
00220 {
00221 record->createWidgetForExercise(true, model, false);
00222 }
00223
00224 p_accountingRecordLayout->addWidget(record);
00225 p_accountingRecordList.append(record);
00226 saveBeforeActualizingAccounts();
00227 }
00228
00229 void AccountingRecordWidget::saveBeforeActualizingAccounts()
00230 {
00231 qDebug()<<"saveBeforeActualizingAccounts";
00232 exercise->dataChanged();
00233
00234 if(showSolution)
00235 {
00236 exercise->solutionRecords()->clear();
00237 }
00238 else
00239 {
00240 exercise->accountingRecords()->clear();
00241 }
00242
00243
00244 for(int i = 0; i < p_accountingRecordList.size(); i++)
00245 {
00246 AccountingRecord *current = p_accountingRecordList.value(i);
00247 current->saveIntoData(exercise);
00248 }
00249 emit actualizeAccountsAfterSaving();
00250 }
00251
00252 void AccountingRecordWidget::correctAccountingRecord()
00253 {
00254 exercise->dataChanged();
00255 exercise->accountingRecords()->clear();
00256 validator->resetOk();
00257 resetAllStylesForValidator();
00258 for(int i = 0; i < p_accountingRecordList.size(); i++)
00259 {
00260 AccountingRecord *current = p_accountingRecordList.value(i);
00261 current->saveIntoData(exercise);
00262
00263 emit beginToValidate(exercise, current->accountingRowInfos(), current->creditList(), current->debitList());
00264 }
00265 if(validator->everythingOk())
00266 {
00267 emit changeState(exercise->name(), exercise->id(), Correct);
00268 }
00269 else
00270 {
00271 emit changeState(exercise->name(), exercise->id(), Wrong);
00272 }
00273 }
00274
00275 void AccountingRecordWidget::deleteWidget()
00276 {
00277 QLayoutItem *item;
00278 while(item = p_layoutForAll->takeAt(0))
00279 {
00280 if((item->widget()) != 0)
00281 {
00282 qobject_cast<AccountingRecord *>(item->widget())->deleteWidget();
00283 p_accountingRecordList.clear();
00284 }
00285 else
00286 {
00287 QLayout *layout = item->layout();
00288 while(item = layout->takeAt(0))
00289 {
00290 if((item->widget()) != 0)
00291 {
00292 layout->removeWidget(item->widget());
00293 delete item->widget();
00294 }
00295 }
00296 delete layout;
00297 }
00298 }
00299 p_accountingRecordList.clear();
00300 }
00301
00302 void AccountingRecordWidget::resetAllStylesForValidator()
00303 {
00304 for(int i = 0; i < p_accountingRecordList.count(); i++)
00305 {
00306 AccountingRecord *record = p_accountingRecordList.value(i);
00307 record->resetStyle();
00308 }
00309 }