kplato

kpttaskcostpanel.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2005 Dag Andersen <danders@get2net.dk>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library Cost Public
6 License as published by the Free Software Foundation;
7 version 2 of the License.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library Cost Public License for more details.
13
14 You should have received a copy of the GNU Library Cost Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "kpttaskcostpanel.h"
21#include "kptaccount.h"
22#include "kpttask.h"
23#include "kptcommand.h"
24#include "kptpart.h"
25
26#include <tdemessagebox.h>
27#include <klineedit.h>
28#include <kcombobox.h>
29#include <tdelocale.h>
30#include <kcommand.h>
31
32#include <kdebug.h>
33
34namespace KPlato
35{
36
37TaskCostPanel::TaskCostPanel(Task &task, Accounts &accounts, TQWidget *p, const char *n)
38 : TaskCostPanelImpl(p, n),
39 m_task(task),
40 m_accounts(accounts) {
41
42 m_accountList << i18n("None");
43 m_accountList += accounts.costElements();
44 setStartValues(task);
45}
46
47void TaskCostPanel::setStartValues(Task &task) {
48 runningAccount->insertStringList(m_accountList);
49 m_oldrunning = m_accounts.findRunningAccount(task);
50 if (m_oldrunning) {
51 setCurrentItem(runningAccount, m_oldrunning->name());
52 }
53
54 startupCost->setText(TDEGlobal::locale()->formatMoney(task.startupCost()));
55 startupAccount->insertStringList(m_accountList);
56 m_oldstartup = m_accounts.findStartupAccount(task);
57 if (m_oldstartup) {
58 setCurrentItem(startupAccount, m_oldstartup->name());
59 }
60
61 shutdownCost->setText(TDEGlobal::locale()->formatMoney(task.shutdownCost()));
62 shutdownAccount->insertStringList(m_accountList);
63 m_oldshutdown = m_accounts.findShutdownAccount(task);
64 if (m_oldshutdown) {
65 setCurrentItem(shutdownAccount, m_oldshutdown->name());
66 }
67}
68
69void TaskCostPanel::setCurrentItem(TQComboBox *box, TQString name) {
70 box->setCurrentItem(0);
71 for (int i = 0; i < box->count(); ++i) {
72 if (name == box->text(i)) {
73 box->setCurrentItem(i);
74 break;
75 }
76 }
77}
78
79KCommand *TaskCostPanel::buildCommand(Part *part) {
80 KMacroCommand *cmd = new KMacroCommand(i18n("Modify Task Cost"));
81 bool modified = false;
82
83 if ((m_oldrunning == 0 && runningAccount->currentItem() != 0) ||
84 (m_oldrunning && m_oldrunning->name() != runningAccount->currentText())) {
85 cmd->addCommand(new NodeModifyRunningAccountCmd(part, m_task, m_oldrunning, m_accounts.findAccount(runningAccount->currentText())));
86 modified = true;
87 }
88 if ((m_oldstartup == 0 && startupAccount->currentItem() != 0) ||
89 (m_oldstartup && m_oldstartup->name() != startupAccount->currentText())) {
90 cmd->addCommand(new NodeModifyStartupAccountCmd(part, m_task, m_oldstartup, m_accounts.findAccount(startupAccount->currentText())));
91 modified = true;
92 }
93 if ((m_oldshutdown == 0 && shutdownAccount->currentItem() != 0) ||
94 (m_oldshutdown && m_oldshutdown->name() != shutdownAccount->currentText())) {
95 cmd->addCommand(new NodeModifyShutdownAccountCmd(part, m_task, m_oldshutdown, m_accounts.findAccount(shutdownAccount->currentText())));
96 modified = true;
97 }
98 double money = TDEGlobal::locale()->readMoney(startupCost->text());
99 if (money != m_task.startupCost()) {
100 cmd->addCommand(new NodeModifyStartupCostCmd(part, m_task, money));
101 modified = true;
102 }
103 money = TDEGlobal::locale()->readMoney(shutdownCost->text());
104 if (money != m_task.shutdownCost()) {
105 cmd->addCommand(new NodeModifyShutdownCostCmd(part, m_task, money));
106 modified = true;
107 }
108 if (!modified) {
109 delete cmd;
110 return 0;
111 }
112 return cmd;
113}
114
115bool TaskCostPanel::ok() {
116 if (runningAccount->currentItem() == 0 ||
117 m_accounts.findAccount(runningAccount->currentText()) == 0) {
118 //message
119 return false;
120 }
121 if (startupAccount->currentItem() == 0 ||
122 m_accounts.findAccount(startupAccount->currentText()) == 0) {
123 //message
124 return false;
125 }
126 if (shutdownAccount->currentItem() == 0 ||
127 m_accounts.findAccount(shutdownAccount->currentText()) == 0) {
128 //message
129 return false;
130 }
131 return true;
132}
133
134
135TaskCostPanelImpl::TaskCostPanelImpl(TQWidget *p, const char *n)
136 : TaskCostPanelBase(p, n)
137{
138 connect(runningAccount, TQ_SIGNAL(activated(int)), TQ_SLOT(slotChanged()));
139 connect(startupAccount, TQ_SIGNAL(activated(int)), TQ_SLOT(slotChanged()));
140 connect(shutdownAccount, TQ_SIGNAL(activated(int)), TQ_SLOT(slotChanged()));
141 connect(startupCost, TQ_SIGNAL(textChanged(const TQString&)), TQ_SLOT(slotChanged()));
142 connect(shutdownCost, TQ_SIGNAL(textChanged(const TQString&)), TQ_SLOT(slotChanged()));
143}
144
145void TaskCostPanelImpl::slotChanged() {
146 emit changed();
147}
148
149} //KPlato namespace
150
151#include "kpttaskcostpanel.moc"
Accounts administrates all accounts.
Definition kptaccount.h:180
A task in the scheduling software is represented by this class.
Definition kpttask.h:40