1 | #include <iostream> |
---|
2 | |
---|
3 | #include "GWt_dropZoneLigneFaiseau.h" |
---|
4 | #include "nomdElements.h" |
---|
5 | #include "GWt_tools.h" |
---|
6 | |
---|
7 | #include <Wt/WCssDecorationStyle> |
---|
8 | #include <Wt/WSignalMapper> |
---|
9 | #include <Wt/WText> |
---|
10 | #include <Wt/WImage> |
---|
11 | #include <Wt/WHBoxLayout> |
---|
12 | #include <Wt/WGridLayout> |
---|
13 | |
---|
14 | GWt_dropZoneLigneFaiseau::GWt_dropZoneLigneFaiseau(PspaApplication* ps,bool linkImageOk, bool showInsertMessage) |
---|
15 | : WContainerWidget(), |
---|
16 | pspa_(ps), |
---|
17 | insertMessage_(NULL), |
---|
18 | linkImage_(NULL), |
---|
19 | abstractElement_(NULL) |
---|
20 | { |
---|
21 | resize(5,5); |
---|
22 | cout<<"new GWt_dropZoneLigneFaiseau : "<< this << endl; |
---|
23 | |
---|
24 | // connect mouse events |
---|
25 | |
---|
26 | // setMouseOverDelay(100); |
---|
27 | mouseWentOver().connect(this, &GWt_dropZoneLigneFaiseau::mouseOver); |
---|
28 | mouseWentUp().connect(this, &GWt_dropZoneLigneFaiseau::mouseUp); |
---|
29 | mouseWentDown().connect(this, &GWt_dropZoneLigneFaiseau::mouseDown); |
---|
30 | mouseWentOut().connect(this, &GWt_dropZoneLigneFaiseau::mouseOut); |
---|
31 | mouseDragged().connect(this, &GWt_dropZoneLigneFaiseau::mouseIsDragged); |
---|
32 | |
---|
33 | WContainerWidget* insertMessage_ = new WContainerWidget(); |
---|
34 | WGridLayout *textGridLayout = new WGridLayout(); |
---|
35 | |
---|
36 | WText* insertMessageText = new WText("Drag elements here",insertMessage_); |
---|
37 | |
---|
38 | insertMessageText->decorationStyle().setBorder (WBorder (WBorder::Dashed)); |
---|
39 | decorationStyle().setBorder (WBorder (WBorder::Dotted)); |
---|
40 | |
---|
41 | textGridLayout->addWidget(new WText(" "),0,0); |
---|
42 | textGridLayout->addWidget(insertMessageText,1,0); |
---|
43 | textGridLayout->addWidget(new WText(" "),2,0); |
---|
44 | insertMessage_->setLayout(textGridLayout); |
---|
45 | |
---|
46 | insertMessage_->setContentAlignment( Wt::AlignCenter |Wt::AlignMiddle ); |
---|
47 | textGridLayout->setRowStretch(0, 1); |
---|
48 | textGridLayout->setRowStretch(2, 1); |
---|
49 | |
---|
50 | addWidget(insertMessage_); |
---|
51 | if (linkImageOk) { |
---|
52 | linkImage_ = new WImage("icons/link.jpg"); |
---|
53 | linkImage_->setMargin(4, Top | Bottom); // add margin vertically |
---|
54 | addWidget(linkImage_); |
---|
55 | } |
---|
56 | |
---|
57 | if (! showInsertMessage) { |
---|
58 | insertMessage_->hide(); |
---|
59 | } |
---|
60 | |
---|
61 | // accept drops |
---|
62 | int nElts= nomdElements::getNumberOfElements(); |
---|
63 | for(int k = 0; k < nElts; k++) { |
---|
64 | typedElement eType= (typedElement)k; |
---|
65 | acceptDrops(nomdElements::getImageFromType(eType)); |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | GWt_dropZoneLigneFaiseau::~GWt_dropZoneLigneFaiseau() |
---|
73 | { |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | void GWt_dropZoneLigneFaiseau::mouseOver(const WMouseEvent& e) |
---|
78 | { |
---|
79 | // Add text |
---|
80 | #ifdef PSPA_DEBUG |
---|
81 | printf("button %d\n",WMouseEvent::LeftButton); |
---|
82 | #endif |
---|
83 | if (insertMessage_) { |
---|
84 | // only if mouse is pressed |
---|
85 | if (e.button () == WMouseEvent::LeftButton) { |
---|
86 | if (!abstractElement_) { |
---|
87 | insertMessage_->show(); |
---|
88 | resize(insertMessage_->width(),insertMessage_->height()); |
---|
89 | } |
---|
90 | } else { |
---|
91 | #ifdef PSPA_DEBUG |
---|
92 | GWt_tools::addConsoleMessage("GWt_dropZoneLigneFaiseau::mouseOver but no button press"); |
---|
93 | #endif |
---|
94 | } |
---|
95 | } |
---|
96 | if (linkImage_) { |
---|
97 | linkImage_->hide(); |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | #ifdef PSPA_DEBUG |
---|
102 | GWt_tools::addConsoleMessage("GWt_dropZoneLigneFaiseau::mouseOver"); |
---|
103 | #endif |
---|
104 | } |
---|
105 | |
---|
106 | void GWt_dropZoneLigneFaiseau::mouseOut(const WMouseEvent& e) |
---|
107 | { |
---|
108 | if (!abstractElement_) { |
---|
109 | if (linkImage_) { |
---|
110 | resize(10,32); // image size |
---|
111 | } else { |
---|
112 | resize(10,10); |
---|
113 | } |
---|
114 | } |
---|
115 | if (insertMessage_) { |
---|
116 | insertMessage_->hide(); |
---|
117 | } |
---|
118 | if (linkImage_) { |
---|
119 | linkImage_->show(); |
---|
120 | } |
---|
121 | |
---|
122 | #ifdef PSPA_DEBUG |
---|
123 | GWt_tools::addConsoleMessage("GWt_dropZoneLigneFaiseau::mouseOut"); |
---|
124 | #endif |
---|
125 | } |
---|
126 | |
---|
127 | void GWt_dropZoneLigneFaiseau::mouseIsDragged(const WMouseEvent& e) |
---|
128 | { |
---|
129 | #ifdef PSPA_DEBUG |
---|
130 | GWt_tools::addConsoleMessage(" GWt_dropZoneLigneFaiseau::mouseDragged"); |
---|
131 | #endif |
---|
132 | } |
---|
133 | |
---|
134 | void GWt_dropZoneLigneFaiseau::mouseUp(const WMouseEvent& e) |
---|
135 | { |
---|
136 | #ifdef PSPA_DEBUG |
---|
137 | GWt_tools::addConsoleMessage("GWt_dropZoneLigneFaiseau::mouseOut up"); |
---|
138 | #endif |
---|
139 | } |
---|
140 | |
---|
141 | void GWt_dropZoneLigneFaiseau::mouseDown(const WMouseEvent& e) |
---|
142 | { |
---|
143 | #ifdef PSPA_DEBUG |
---|
144 | GWt_tools::addConsoleMessage("GWt_dropZoneLigneFaiseau::mouseDown up"); |
---|
145 | #endif |
---|
146 | } |
---|
147 | |
---|
148 | void GWt_dropZoneLigneFaiseau::dropEvent(WDropEvent evt) |
---|
149 | { |
---|
150 | #ifdef PSPA_DEBUG |
---|
151 | GWt_tools::addConsoleMessage(" GWt_dropZoneLigneFaiseau::dropEvent"); |
---|
152 | #endif |
---|
153 | |
---|
154 | nomdElements e(evt.mimeType()); |
---|
155 | typedElement eType = e.getElementType(); |
---|
156 | |
---|
157 | // update source if necessary |
---|
158 | cout<<"dynamic_cast SOURCE------" << evt.source() << endl; |
---|
159 | if (dynamic_cast <GWt_dropZoneLigneFaiseau*> (evt.source())) { |
---|
160 | cout<<"OKIKKKIIII" << dynamic_cast <GWt_dropZoneLigneFaiseau*> (evt.source()) << endl; |
---|
161 | removeOtherDropZone(dynamic_cast <GWt_dropZoneLigneFaiseau*> (evt.source())); |
---|
162 | } |
---|
163 | |
---|
164 | if(eType == inconnu) { |
---|
165 | cout << "GWt_LigneFaisceau::element type " << eType << " inconnu " << endl; |
---|
166 | return; |
---|
167 | } |
---|
168 | |
---|
169 | GWt_tools::addConsoleMessage(e.getElementName()); |
---|
170 | |
---|
171 | abstractElement* ptr = pspa_->getDataManager()->addElement(eType); |
---|
172 | |
---|
173 | ptr->setLabel(getElementNumberInBeamLine(e)); |
---|
174 | |
---|
175 | GWt_abstractElement* insertedAbstractElement = GWt_abstractElement::ajoute(ptr); |
---|
176 | cout << " je vais ajouter l'element " << ptr->getNomdElement().getElementName() << " de type " << ptr->getNomdElement().getElementType() << "ID "<< insertedAbstractElement << endl; |
---|
177 | |
---|
178 | if(insertedAbstractElement == NULL) return; |
---|
179 | insertedAbstractElement->setLabelWidget(); |
---|
180 | |
---|
181 | // ////////////////////////////////////////////////////////////////// |
---|
182 | // // pourVoir |
---|
183 | |
---|
184 | // cout<<"container: "<<gw->getWidget()->id()<<endl; |
---|
185 | // cout << "number of widgets in this container= " << insertedAbstractElement->getWidget()->count() << ": "; |
---|
186 | // for(int k = 0; k < insertedAbstractElement->getWidget()->count(); k++) { |
---|
187 | // WWidget *w= insertedAbstractElement->getWidget()->widget(k); |
---|
188 | // cout << w->id() << ", "; |
---|
189 | // } |
---|
190 | // cout << endl; |
---|
191 | |
---|
192 | // ////////////////////////////////////////////////////////////////// |
---|
193 | |
---|
194 | WSignalMapper<GWt_abstractElement *> *mapper= new WSignalMapper<GWt_abstractElement *>(this); |
---|
195 | mapper->mapped().connect(this,&GWt_dropZoneLigneFaiseau::onClick); |
---|
196 | mapper->mapConnect(insertedAbstractElement->getImage()->doubleClicked(),insertedAbstractElement); |
---|
197 | |
---|
198 | ////////////////////////////////////////////////////////////////// |
---|
199 | |
---|
200 | // add the dropped widget |
---|
201 | printf("add the dropped widget\n"); |
---|
202 | |
---|
203 | // add drop zone before and after |
---|
204 | if (static_cast <WContainerWidget*> (parent())) { |
---|
205 | WContainerWidget* wc = static_cast <WContainerWidget*> (parent()); |
---|
206 | |
---|
207 | cout<<"add the dropped parent %d"<< wc << " COUNT:"<< count () <<"<---------------"<< endl; |
---|
208 | // do not insert new element if some element is already present |
---|
209 | |
---|
210 | WBoxLayout* layout = ((WBoxLayout*)(wc->layout())); |
---|
211 | bool replaceElement = false; |
---|
212 | |
---|
213 | if (abstractElement_) { |
---|
214 | replaceElement = true; |
---|
215 | } |
---|
216 | |
---|
217 | // insert one before |
---|
218 | if (!replaceElement) { |
---|
219 | // if it the first element of the beam line |
---|
220 | if (layout->indexOf ((WLayoutItem*)(layout->findWidgetItem (this))) == 0) { |
---|
221 | layout->insertWidget (layout->indexOf ((WLayoutItem*)(layout->findWidgetItem (this))), new GWt_dropZoneLigneFaiseau(pspa_,false)); |
---|
222 | |
---|
223 | } else { |
---|
224 | layout->insertWidget (layout->indexOf ((WLayoutItem*)(layout->findWidgetItem (this))), new GWt_dropZoneLigneFaiseau(pspa_,true)); |
---|
225 | } |
---|
226 | } |
---|
227 | // FIXME Replace |
---|
228 | |
---|
229 | // complete widget |
---|
230 | clear(); |
---|
231 | insertMessage_ = NULL; |
---|
232 | linkImage_ = NULL; |
---|
233 | abstractElement_ = insertedAbstractElement->getWidget(); |
---|
234 | addWidget(abstractElement_); |
---|
235 | // change strech (useful if it was the last one) |
---|
236 | layout->setStretchFactor(this,0); |
---|
237 | |
---|
238 | // change background |
---|
239 | decorationStyle().setBackgroundColor (WColor("white")); |
---|
240 | |
---|
241 | // remove border |
---|
242 | decorationStyle().setBorder (WBorder (WBorder::None)); |
---|
243 | |
---|
244 | |
---|
245 | // insert after |
---|
246 | // if it is the last, add a strech factor and no link image |
---|
247 | cout<<" nb elem:"<< layout->indexOf ((WLayoutItem*)(layout->findWidgetItem (this)))+1 << " COUNT:"<< wc->count () <<"<---------------ID:"<< abstractElement_ << endl; |
---|
248 | if (!replaceElement) { |
---|
249 | if (layout->indexOf ((WLayoutItem*)(layout->findWidgetItem (this)))+1 == wc->count()) { |
---|
250 | layout->insertWidget (layout->indexOf ((WLayoutItem*)(layout->findWidgetItem (this)))+1, new GWt_dropZoneLigneFaiseau(pspa_,false),1); |
---|
251 | } else { |
---|
252 | layout->insertWidget (layout->indexOf ((WLayoutItem*)(layout->findWidgetItem (this)))+1, new GWt_dropZoneLigneFaiseau(pspa_)); |
---|
253 | } |
---|
254 | } |
---|
255 | |
---|
256 | /* |
---|
257 | * Set the image to be draggable, showing the other image (dragImage) |
---|
258 | * to be used as the widget that is visually dragged. |
---|
259 | */ |
---|
260 | WImage *dragImage = new WImage(e.getImageFromType(eType).c_str(),this); |
---|
261 | |
---|
262 | // setDraggable(evt.mimeType(),dragImage,true);//,insertedAbstractElement->getButton(),true); |
---|
263 | abstractElement_->setDraggable(evt.mimeType(),dragImage,true, this); |
---|
264 | |
---|
265 | |
---|
266 | cout<<"add the dropped parent %d"<< wc << " COUNT:"<< wc->count () <<"<---------------"<< endl; |
---|
267 | } |
---|
268 | |
---|
269 | |
---|
270 | |
---|
271 | //insertedAbstractElement->getWidget()->decorationStyle().setBackgroundColor (WColor("blue")); |
---|
272 | pspa_->updateSelections(); |
---|
273 | |
---|
274 | // ////////////////////////////////////////////////////////////////// |
---|
275 | // // pourVoir |
---|
276 | |
---|
277 | // cout << "number of items in the layout= " << hbox_->count() << ": "; |
---|
278 | // for(int k = 0; k < hbox_->count(); k++) |
---|
279 | // { |
---|
280 | // WLayoutItem *wi= hbox_->itemAt(k); |
---|
281 | // WWidget *w= wi->widget(); |
---|
282 | // cout << w->id() << ", "; |
---|
283 | // } |
---|
284 | // cout << endl; |
---|
285 | |
---|
286 | // ////////////////////////////////////////////////////////////////// |
---|
287 | // // pourVoir |
---|
288 | |
---|
289 | // int nElts= pspa_->getDataManager()->beamLineSize(); |
---|
290 | // cout << "GWt_LigneFaisceau:: nElts= " << nElts << endl; |
---|
291 | // unsigned int k; |
---|
292 | // for(k = 0; k < (unsigned)nElts; k++) { |
---|
293 | // abstractElement* ptr= pspa_->getDataManager()->getCollection()->getElementPointer(k); |
---|
294 | // string label= ptr->getLabel(); |
---|
295 | // cout << "GWt_LigneFaisceau:: " << label << " dans la liste" << endl; |
---|
296 | // } |
---|
297 | |
---|
298 | } |
---|
299 | |
---|
300 | |
---|
301 | void GWt_dropZoneLigneFaiseau::onClick(GWt_abstractElement* gw) |
---|
302 | { |
---|
303 | cout << "mouse key was double clicked on this widget " << gw->getWidget()->id()<< endl; |
---|
304 | |
---|
305 | messageDropZone_ = new GWt_dialog("Question","Are you sure you want to delete the selected item?",GWt_dialog::Warning,true,true); |
---|
306 | |
---|
307 | WSignalMapper<GWt_abstractElement *> *MyMap = new WSignalMapper<GWt_abstractElement *>(this); |
---|
308 | MyMap->mapped().connect(this, &GWt_dropZoneLigneFaiseau::removeDone); |
---|
309 | MyMap->mapConnect(messageDropZone_->finished(),gw); |
---|
310 | messageDropZone_->exec(); |
---|
311 | } |
---|
312 | |
---|
313 | |
---|
314 | void GWt_dropZoneLigneFaiseau::removeDone(GWt_abstractElement* gw) |
---|
315 | { |
---|
316 | if (messageDropZone_->result() == WDialog::Accepted) { |
---|
317 | // FIXME |
---|
318 | // removeDropZone(); |
---|
319 | } |
---|
320 | } |
---|
321 | |
---|
322 | |
---|
323 | |
---|
324 | void GWt_dropZoneLigneFaiseau::removeOtherDropZone(GWt_dropZoneLigneFaiseau* other) |
---|
325 | { |
---|
326 | // 3 cases : |
---|
327 | // - first element : remove elem + link after |
---|
328 | // - last element : remove elem + link before |
---|
329 | // - middle element : remove elem + link after/before |
---|
330 | |
---|
331 | WContainerWidget* wc = static_cast <WContainerWidget*> (parent()); |
---|
332 | WBoxLayout* layout = ((WBoxLayout*)(wc->layout())); |
---|
333 | |
---|
334 | #ifdef PSPA_DEBUG |
---|
335 | GWt_tools::addConsoleMessage("remove from drag Zone"); |
---|
336 | cout << "remove from drag Zone id" << layout->indexOf ((WLayoutItem*)(layout->findWidgetItem (other))) << " count:" << layout->count()<< endl; |
---|
337 | #endif |
---|
338 | // first element |
---|
339 | if (layout->indexOf ((WLayoutItem*)(layout->findWidgetItem (other))) == 1) { |
---|
340 | if (layout->itemAt (2)) { |
---|
341 | layout->removeItem (layout->itemAt(2)); |
---|
342 | #ifdef PSPA_DEBUG |
---|
343 | GWt_tools::addConsoleMessage("remove first link"); |
---|
344 | #endif |
---|
345 | } |
---|
346 | |
---|
347 | // last and middle |
---|
348 | } else if (layout->indexOf ((WLayoutItem*)(layout->findWidgetItem (other))) == layout->count()-2) { |
---|
349 | if (layout->itemAt (layout->count()-3)) { |
---|
350 | layout->removeItem (layout->itemAt(layout->count()-3)); |
---|
351 | #ifdef PSPA_DEBUG |
---|
352 | GWt_tools::addConsoleMessage("remove last link"); |
---|
353 | #endif |
---|
354 | } |
---|
355 | } |
---|
356 | |
---|
357 | // remove the drop zone |
---|
358 | if (layout->findWidgetItem (other)) { |
---|
359 | layout->removeItem (layout->itemAt(layout->indexOf((WLayoutItem*)(layout->findWidgetItem (other))))); |
---|
360 | #ifdef PSPA_DEBUG |
---|
361 | GWt_tools::addConsoleMessage("remove drop zone"); |
---|
362 | #endif |
---|
363 | } |
---|
364 | |
---|
365 | |
---|
366 | // FIXME ne mas passer par un label mais par un abstrastElement |
---|
367 | // pspa_->getDataManager()->getCollection()->eraseElement(label); |
---|
368 | pspa_->updateSelections(); |
---|
369 | } |
---|
370 | |
---|
371 | |
---|
372 | |
---|
373 | void GWt_dropZoneLigneFaiseau::addElement(WContainerWidget* elem){ |
---|
374 | |
---|
375 | clear(); |
---|
376 | abstractElement_ = elem; |
---|
377 | addWidget(abstractElement_); |
---|
378 | if (static_cast <WContainerWidget*> (parent())) { |
---|
379 | WContainerWidget* wc = static_cast <WContainerWidget*> (parent()); |
---|
380 | WBoxLayout* layout = ((WBoxLayout*)(wc->layout())); |
---|
381 | layout->setStretchFactor(this,0); |
---|
382 | } |
---|
383 | |
---|
384 | |
---|
385 | insertMessage_ = NULL; |
---|
386 | linkImage_ = NULL; |
---|
387 | } |
---|
388 | |
---|
389 | std::string GWt_dropZoneLigneFaiseau::getElementNumberInBeamLine(nomdElements elem){ |
---|
390 | string str = ""; |
---|
391 | |
---|
392 | if (static_cast <WContainerWidget*> (parent())) { |
---|
393 | WContainerWidget* wc = static_cast <WContainerWidget*> (parent()); |
---|
394 | for (int a=0; a<wc->count(); a++) { |
---|
395 | if (static_cast <GWt_abstractElement*> (wc->widget (a))) { |
---|
396 | GWt_abstractElement* ab = static_cast <GWt_abstractElement*> (wc->widget (a)); |
---|
397 | /* |
---|
398 | printf("1--->%d\n",ab->getElement()); |
---|
399 | printf("2--->%d\n",ab->getElement()->getLenghtOfElement()); |
---|
400 | printf("3--->%d\n",ab->getElement()->getName().getElementType()); |
---|
401 | printf("4--->%d\n",elem.getElementType()); |
---|
402 | |
---|
403 | if (static_cast <GWt_abstractElement*> (wc->widget (a))->getElement()->getName().getElementType() == elem.getElementType()) { |
---|
404 | pspa_->addConsoleMessage("type trouvé"); |
---|
405 | } else { |
---|
406 | pspa_->addConsoleMessage("type trouvé NOT "); |
---|
407 | } |
---|
408 | */ |
---|
409 | |
---|
410 | } |
---|
411 | } |
---|
412 | |
---|
413 | int n = 999999; |
---|
414 | |
---|
415 | std::stringstream stream; |
---|
416 | stream << n ; |
---|
417 | std::string number(stream.str()); |
---|
418 | |
---|
419 | if(n < 10) { |
---|
420 | str= elem.getElementLabel()+"0"+number; |
---|
421 | } else { |
---|
422 | str= elem.getElementLabel()+number; |
---|
423 | } |
---|
424 | } |
---|
425 | |
---|
426 | return str; |
---|
427 | } |
---|