1 
2 
3 #include "tfarmcontroller.h"
4 #include "application.h"
5 #include "dependedlist.h"
6 #include "submitpage.h"
7 
8 #include "tw/message.h"
9 
10 #include <vector>
11 
12 using namespace std;
13 
DependedList(TWidget * parent)14 DependedList::DependedList(TWidget *parent) : TWidget(parent) {
15   m_depList = new TTextList(this);
16 
17   m_add = new TButton(this, "Add");
18   tconnect<DependedList>(*(m_add), this, onAdd);
19 
20   m_remove = new TButton(this, "Remove");
21   tconnect<DependedList>(*(m_remove), this, onRemove);
22 }
23 
24 //------------------------------------------------------------------------------
25 
configureNotify(const TDimension & size)26 void DependedList::configureNotify(const TDimension &size) {
27   const int leftSize = size.lx / 3;
28   m_depList->setGeometry(0, 0, leftSize, size.ly - 1);
29 
30   TDimension buttonSize(60, 15);
31   int x  = leftSize + 30;
32   int y0 = size.ly - 10;
33 
34   m_add->setGeometry(x, y0 - buttonSize.ly, x + buttonSize.lx, y0);
35   y0 -= (buttonSize.ly + 10);
36   m_remove->setGeometry(x, y0 - buttonSize.ly, x + buttonSize.lx, y0);
37 }
38 
39 //------------------------------------------------------------------------------
40 
onRemove()41 void DependedList::onRemove() {
42   if (!m_depList->getSelectedItem(0)) return;
43 
44   int count = m_depList->getSelectedItemCount();
45   int i     = 0;
46   for (; i < count; ++i) {
47     string id = m_depList->getSelectedItemId(i);
48     m_tasks.erase(m_tasks.find(id));
49     m_depList->removeItem(id);
50   }
51 
52   m_depList->invalidate();
53 
54   SubmitPage *submitPage = dynamic_cast<SubmitPage *>(getParent());
55   if (submitPage) {
56     SubmitPageTask *task = submitPage->getTask();
57     if (task) task->setDependencies(m_tasks);
58   }
59 }
60 
61 //------------------------------------------------------------------------------
62 
clearAll()63 void DependedList::clearAll() {
64   m_depList->clearAll();
65   m_tasks.erase(m_tasks.begin(), m_tasks.end());
66 }
67 
68 //------------------------------------------------------------------------------
69 
onAdd()70 void DependedList::onAdd() {
71   static DependedPopup *popup = 0;
72 
73   if (!popup) {
74     popup = new DependedPopup(this);
75 
76     popup->setOkAction(
77         new TDependedPopupAction<DependedList>(this, &DependedList::AddItems));
78   }
79 
80   if (!popup) return;
81 
82   TFarmController *controller = Application::instance()->getController();
83   vector<TaskShortInfo> tasks;
84   try {
85     controller->getTasks("", tasks);
86   } catch (TException &e) {
87     TMessage::error(toString(e.getMessage()));
88     return;
89   }
90 
91   popup->setList(tasks);
92 
93   TDimension d = TMainshell::getMainshell()->getSize();
94 #ifdef WIN32
95   HDC hdc = GetDC(0);
96   d.lx    = GetDeviceCaps(hdc, HORZRES);
97   d.ly    = GetDeviceCaps(hdc, VERTRES);
98   ReleaseDC(0, hdc);
99 #endif
100 
101   d -= popup->getSize();
102   popup->popup(TPoint(d.lx / 2, d.ly / 2));
103   popup->setCaption("Tasks Submitted");
104 }
105 
106 //------------------------------------------------------------------------------
107 
AddItems(const vector<string> & tasksId)108 void DependedList::AddItems(const vector<string> &tasksId) {
109   TFarmController *controller       = Application::instance()->getController();
110   vector<string>::const_iterator it = tasksId.begin();
111   for (; it != tasksId.end(); ++it)
112     if (m_tasks.end() == m_tasks.find(*it)) {
113       try {
114         string parentId, name;
115         TaskState status;
116         controller->queryTaskShortInfo(*it, parentId, name, status);
117         string label = "<" + *it + "> " + name;
118         m_depList->addItem(new TTextListItem(*it, label));
119         m_tasks.insert(make_pair(*it, label));
120       } catch (TException &e) {
121         TMessage::error(toString(e.getMessage()));
122       }
123     }
124   m_depList->invalidate();
125 
126   SubmitPage *submitPage = dynamic_cast<SubmitPage *>(getParent());
127   if (submitPage) {
128     SubmitPageTask *task = submitPage->getTask();
129     if (task) task->setDependencies(m_tasks);
130   }
131 }
132 
133 //------------------------------------------------------------------------------
134 
setList(const std::map<string,string> & tasks)135 void DependedList::setList(const std::map<string, string> &tasks) {
136   m_depList->clearAll();
137   std::map<string, string>::const_iterator it = tasks.begin();
138   for (; it != tasks.end(); ++it)
139     m_depList->addItem(new TTextListItem((*it).first, (*it).second));
140   m_tasks = tasks;
141   invalidate();
142 }
143 
144 //==============================================================================
145 
DependedPopup(TWidget * parent)146 DependedPopup::DependedPopup(TWidget *parent)
147     : TModalPopup(parent, "DependedList"), m_okAction(0) {
148   m_submitList = new TTextList(this);
149 
150   m_ok = new TButton(this, "Ok");
151   tconnect<DependedPopup>(*(m_ok), this, onOk);
152 
153   m_cancel = new TButton(this, "Cancel");
154   tconnect<DependedPopup>(*(m_cancel), this, TPopup::close);
155 }
156 
157 //------------------------------------------------------------------------------
158 
configureNotify(const TDimension & size)159 void DependedPopup::configureNotify(const TDimension &size) {
160   const int bottomSize = 35;
161   m_submitList->setGeometry(0, bottomSize, size.lx - 1, size.ly - 1);
162 
163   TDimension buttonSize(60, 15);
164   int x1 = 30;
165   int x2 = size.lx - x1;
166   int y0 = bottomSize - 10;
167 
168   m_ok->setGeometry(x1, y0 - buttonSize.ly, x1 + buttonSize.lx, y0);
169   m_cancel->setGeometry(x2 - buttonSize.lx, y0 - buttonSize.ly, x2, y0);
170 }
171 
172 //------------------------------------------------------------------------------
173 
getPreferredSize() const174 TDimension DependedPopup::getPreferredSize() const {
175   return TDimension(400, 300);
176 }
177 
178 //------------------------------------------------------------------------------
179 
onOk()180 void DependedPopup::onOk() {
181   assert(m_okAction);
182   vector<string> tasks;
183   if (m_submitList->getSelectedItem(0)) {
184     int count = m_submitList->getSelectedItemCount();
185     int i     = 0;
186     for (; i < count; ++i) tasks.push_back(m_submitList->getSelectedItemId(i));
187   }
188   try {
189     m_okAction->sendCommand(tasks);
190     close();
191   } catch (TException &e) {
192     TMessage::error(toString(e.getMessage()));
193   }
194 }
195 
196 //------------------------------------------------------------------------------
197 
setList(const vector<TaskShortInfo> & tasks)198 void DependedPopup::setList(const vector<TaskShortInfo> &tasks) {
199   m_submitList->clearAll();
200 
201   vector<TaskShortInfo>::const_iterator it = tasks.begin();
202   for (; it != tasks.end(); ++it) {
203     string label = "<" + (*it).m_id + "> " + (*it).m_name;
204     m_submitList->addItem(new TTextListItem((*it).m_id, label));
205   }
206 }
207 
208 //------------------------------------------------------------------------------
209 
setOkAction(TGenericDependedPopupAction * action)210 void DependedPopup::setOkAction(TGenericDependedPopupAction *action) {
211   m_okAction = action;
212 }
213