00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "kpttaskcostpanel.h"
00021 #include "kptaccount.h"
00022 #include "kpttask.h"
00023 #include "kptcommand.h"
00024 #include "kptpart.h"
00025
00026 #include <kmessagebox.h>
00027 #include <klineedit.h>
00028 #include <kcombobox.h>
00029 #include <klocale.h>
00030 #include <kcommand.h>
00031
00032 #include <kdebug.h>
00033
00034 namespace KPlato
00035 {
00036
00037 TaskCostPanel::TaskCostPanel(Task &task, Accounts &accounts, QWidget *p, const char *n)
00038 : TaskCostPanelImpl(p, n),
00039 m_task(task),
00040 m_accounts(accounts) {
00041
00042 m_accountList << i18n("None");
00043 m_accountList += accounts.costElements();
00044 setStartValues(task);
00045 }
00046
00047 void TaskCostPanel::setStartValues(Task &task) {
00048 runningAccount->addItems(m_accountList);
00049 m_oldrunning = m_accounts.findRunningAccount(task);
00050 if (m_oldrunning) {
00051 setCurrentItem(runningAccount, m_oldrunning->name());
00052 }
00053
00054 startupCost->setText(KGlobal::locale()->formatMoney(task.startupCost()));
00055 startupAccount->addItems(m_accountList);
00056 m_oldstartup = m_accounts.findStartupAccount(task);
00057 if (m_oldstartup) {
00058 setCurrentItem(startupAccount, m_oldstartup->name());
00059 }
00060
00061 shutdownCost->setText(KGlobal::locale()->formatMoney(task.shutdownCost()));
00062 shutdownAccount->addItems(m_accountList);
00063 m_oldshutdown = m_accounts.findShutdownAccount(task);
00064 if (m_oldshutdown) {
00065 setCurrentItem(shutdownAccount, m_oldshutdown->name());
00066 }
00067 }
00068
00069 void TaskCostPanel::setCurrentItem(QComboBox *box, QString name) {
00070 box->setCurrentIndex(0);
00071 for (int i = 0; i < box->count(); ++i) {
00072 if (name == box->text(i)) {
00073 box->setCurrentIndex(i);
00074 break;
00075 }
00076 }
00077 }
00078
00079 KCommand *TaskCostPanel::buildCommand(Part *part) {
00080 KMacroCommand *cmd = new KMacroCommand(i18n("Modify Task Cost"));
00081 bool modified = false;
00082
00083 if ((m_oldrunning == 0 && runningAccount->currentItem() != 0) ||
00084 (m_oldrunning && m_oldrunning->name() != runningAccount->currentText())) {
00085 cmd->addCommand(new NodeModifyRunningAccountCmd(part, m_task, m_oldrunning, m_accounts.findAccount(runningAccount->currentText())));
00086 modified = true;
00087 }
00088 if ((m_oldstartup == 0 && startupAccount->currentItem() != 0) ||
00089 (m_oldstartup && m_oldstartup->name() != startupAccount->currentText())) {
00090 cmd->addCommand(new NodeModifyStartupAccountCmd(part, m_task, m_oldstartup, m_accounts.findAccount(startupAccount->currentText())));
00091 modified = true;
00092 }
00093 if ((m_oldshutdown == 0 && shutdownAccount->currentItem() != 0) ||
00094 (m_oldshutdown && m_oldshutdown->name() != shutdownAccount->currentText())) {
00095 cmd->addCommand(new NodeModifyShutdownAccountCmd(part, m_task, m_oldshutdown, m_accounts.findAccount(shutdownAccount->currentText())));
00096 modified = true;
00097 }
00098 double money = KGlobal::locale()->readMoney(startupCost->text());
00099 if (money != m_task.startupCost()) {
00100 cmd->addCommand(new NodeModifyStartupCostCmd(part, m_task, money));
00101 modified = true;
00102 }
00103 money = KGlobal::locale()->readMoney(shutdownCost->text());
00104 if (money != m_task.shutdownCost()) {
00105 cmd->addCommand(new NodeModifyShutdownCostCmd(part, m_task, money));
00106 modified = true;
00107 }
00108 if (!modified) {
00109 delete cmd;
00110 return 0;
00111 }
00112 return cmd;
00113 }
00114
00115 bool TaskCostPanel::ok() {
00116 if (runningAccount->currentItem() == 0 ||
00117 m_accounts.findAccount(runningAccount->currentText()) == 0) {
00118
00119 return false;
00120 }
00121 if (startupAccount->currentItem() == 0 ||
00122 m_accounts.findAccount(startupAccount->currentText()) == 0) {
00123
00124 return false;
00125 }
00126 if (shutdownAccount->currentItem() == 0 ||
00127 m_accounts.findAccount(shutdownAccount->currentText()) == 0) {
00128
00129 return false;
00130 }
00131 return true;
00132 }
00133
00134
00135 TaskCostPanelImpl::TaskCostPanelImpl(QWidget *p, const char *n)
00136 : QWidget(p)
00137 {
00138 setObjectName(n);
00139 setupUi(this);
00140
00141 connect(runningAccount, SIGNAL(activated(int)), SLOT(slotChanged()));
00142 connect(startupAccount, SIGNAL(activated(int)), SLOT(slotChanged()));
00143 connect(shutdownAccount, SIGNAL(activated(int)), SLOT(slotChanged()));
00144 connect(startupCost, SIGNAL(textChanged(const QString&)), SLOT(slotChanged()));
00145 connect(shutdownCost, SIGNAL(textChanged(const QString&)), SLOT(slotChanged()));
00146 }
00147
00148 void TaskCostPanelImpl::slotChanged() {
00149 emit changed();
00150 }
00151
00152 }
00153
00154 #include "kpttaskcostpanel.moc"