1 /***************************************************************************
2              flostempldialog - dialog to edit templates
3                              -------------------
4     begin                : 2004-15-08
5     copyright            : (C) 2004 by Klaas Freitag
6     email                : freitag@kde.org
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 // include files for Qt
19 #include <QButtonGroup>
20 #include <QRadioButton>
21 #include <QLabel>
22 #include <QString>
23 #include <QComboBox>
24 #include <QTreeWidget>
25 #include <QTreeWidgetItem>
26 #include <QCheckBox>
27 #include <QPushButton>
28 #include <QCloseEvent>
29 #include <QHeaderView>
30 #include <QDialogButtonBox>
31 
32 // include files for KDE
33 #include <QDebug>
34 #include <qmessagebox.h>
35 #include <qcombobox.h>
36 #include <QVBoxLayout>
37 
38 #include "floskeltemplate.h"
39 #include "catalogtemplate.h"
40 #include "flostempldialog.h"
41 #include "unitmanager.h"
42 #include "timecalcpart.h"
43 #include "fixcalcpart.h"
44 #include "portal.h"
45 #include "materialcalcpart.h"
46 #include "matcalcdialog.h"
47 #include "stockmaterial.h"
48 #include "timecalcdialog.h"
49 #include "fixcalcdialog.h"
50 #include "stdsatzman.h"
51 #include "katalogman.h"
52 #include "katalog.h"
53 #include "materialselectdialog.h"
54 #include "defaultprovider.h"
55 
FlosTemplDialog(QWidget * parent,bool modal)56 FlosTemplDialog::FlosTemplDialog( QWidget *parent, bool modal )
57     : QDialog( parent ),
58     m_template(0),
59     m_katalog(0),
60     m_fixCalcDia(0),
61     m_timePartDialog(0),
62     m_matPartDialog(0)
63 {
64   QWidget *w = new QWidget( this );
65   QVBoxLayout *mainLayout = new QVBoxLayout;
66   setLayout(mainLayout);
67   mainLayout->addWidget(w);
68 
69   setupUi( w );
70 
71   setWindowTitle( i18n("Create or Edit Template Items") );
72   setModal( modal );
73   _buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
74   QPushButton *okButton = _buttonBox->button(QDialogButtonBox::Ok);
75   okButton->setDefault(true);
76   okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
77   connect(_buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
78   connect(_buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
79   mainLayout->addWidget(_buttonBox);
80   okButton->setDefault(true);
81 
82   //Initialise the buttongroup to switch between manual and calculated price
83   m_gbPriceSrc = new QButtonGroup(this);
84   m_gbPriceSrc->addButton(m_rbManual, 0);
85   m_gbPriceSrc->addButton(m_rbCalculation, 1);
86 
87   m_timeParts->header()->setResizeMode(QHeaderView::ResizeToContents);
88   m_fixParts->header()->setResizeMode(QHeaderView::ResizeToContents);
89   m_matParts->header()->setResizeMode(QHeaderView::ResizeToContents);
90 
91   // disable for now, not used
92   cbMwst->setVisible(false);
93   m_mwstLabel->setVisible(false);
94   setupConnections();
95   setButtonIcons();
96 }
97 
setupConnections()98 void FlosTemplDialog::setupConnections()
99 {
100   connect(m_gbPriceSrc, SIGNAL(buttonClicked(int)), this, SLOT(slCalcOrFix(int)));
101 
102   /* connect a value Changed signal of the manual price field */
103   connect( m_manualPriceVal, SIGNAL( valueChanged(double)),
104            this, SLOT( slManualPriceChanged(double)));
105 
106   connect( m_text, SIGNAL(textChanged()),this, SLOT(slSetNewText()));
107 
108   connect( spBenefit, SIGNAL(valueChanged(int)), this, SLOT(slBenefitChange(int)));
109 
110 
111   //Time calculation
112   connect(m_butAddTime, SIGNAL(clicked()), this, SLOT(slAddTimePart()));
113   connect(m_butEditTime, SIGNAL(clicked()), this, SLOT(slEditTimePart()));
114   connect(m_butRemoveTime, SIGNAL(clicked()), this, SLOT(slRemoveTimePart()));
115 
116   //Fix costs
117   connect(m_butAddFix, SIGNAL(clicked()), this, SLOT(slAddFixPart()));
118   connect(m_butEditFix, SIGNAL(clicked()), this, SLOT(slEditFixPart()));
119   connect(m_butRemoveFix, SIGNAL(clicked()), this, SLOT(slRemoveFixPart()));
120 
121   //Material
122   connect(m_butAddMat, SIGNAL(clicked()), this, SLOT(slAddMatPart()));
123   connect(m_butEditMat, SIGNAL(clicked()), this, SLOT(slEditMatPart()));
124   connect(m_butRemoveMat, SIGNAL(clicked()), this, SLOT(slRemoveMatPart()));
125 }
126 
setButtonIcons()127 void FlosTemplDialog::setButtonIcons()
128 {
129   m_butAddTime->setIcon(QIcon::fromTheme("list-add"));
130   m_butEditTime->setIcon(QIcon::fromTheme("document-edit"));
131   m_butRemoveTime->setIcon(QIcon::fromTheme("list-remove"));
132 
133   m_butAddFix->setIcon(QIcon::fromTheme("list-add"));
134   m_butEditFix->setIcon(QIcon::fromTheme("document-edit"));
135   m_butRemoveFix->setIcon(QIcon::fromTheme("list-remove"));
136 
137   m_butAddMat->setIcon(QIcon::fromTheme("list-add"));
138   m_butEditMat->setIcon(QIcon::fromTheme("document-edit"));
139   m_butRemoveMat->setIcon(QIcon::fromTheme("list-remove"));
140 }
141 
setTemplate(FloskelTemplate * t,const QString & katalogname,bool newTempl)142 void FlosTemplDialog::setTemplate( FloskelTemplate *t, const QString& katalogname, bool newTempl )
143 {
144   if( ! t ) return;
145   m_template = t;
146   m_templateIsNew = newTempl;
147 
148   m_katalog = KatalogMan::self()->getKatalog(katalogname);
149 
150   if( m_katalog == 0 ) {
151     // qDebug () << "ERR: Floskel Dialog called without valid Katalog!" << endl;
152     return;
153   }
154 
155   QList<CatalogChapter> chapters = m_katalog->getKatalogChapters( );
156   QStringList chapNames;
157   foreach( CatalogChapter chap, chapters ) {
158     chapNames.append( chap.name() );
159   }
160   cbChapter->insertItems(-1, chapNames );
161   int chapID = t->chapterId().toInt();
162   QString chap = m_katalog->chapterName(dbID(chapID));
163   cbChapter->setCurrentIndex(cbChapter->findText( chap ));
164 
165   m_manualPriceVal->setSuffix(m_katalog->locale()->currencySymbol());
166 
167   /* Text of the template */
168   m_text->setText( t->getText());
169 
170   /* Unit */
171   m_unit->clear();
172   m_unit->insertItems(-1, UnitManager::self()->allUnits());
173   m_unit->setCurrentIndex(m_unit->findText( m_template->unit().einheitSingular() ));
174 
175   m_manualPriceVal->setValue( t->unitPrice().toDouble());
176 
177   /* Kind of Calculation: Manual or calculated?  */
178   _origCalcType = m_template->calcKind();
179   if( t->calcKind() == CatalogTemplate::ManualPrice ) {
180     slCalcOrFix(0);
181     m_rbManual->setChecked(true);
182     m_rbCalculation->setChecked(false);
183   } else {
184     slCalcOrFix(1);
185     m_rbManual->setChecked(false);
186     m_rbCalculation->setChecked(true);
187   }
188 
189   /* set up the different calculation parts */
190   setCalcparts();
191   _calcPartsModified = false;
192 
193   /* set text */
194   slSetNewText();
195 
196   m_addTime->setChecked( m_template->hasTimeslice() );
197   m_text->setFocus();
198   m_text->selectAll();
199 
200 }
201 
setCalcparts()202 void FlosTemplDialog::setCalcparts( )
203 {
204   /* time calculation in widget m_timeParts */
205   CalcPartList tpList = m_template->getCalcPartsList( KALKPART_TIME );
206   m_timeParts->clear();
207 
208   QListIterator<CalcPart*> it( tpList );
209   while( it.hasNext() ) {
210 
211     TimeCalcPart *cp = static_cast<TimeCalcPart*>(it.next());
212 
213     QString stdStd = i18n("No");
214     if( cp->globalStdSetAllowed() ) stdStd = i18n("Yes");
215 
216     QTreeWidgetItem *lvItem = new QTreeWidgetItem( m_timeParts );
217     drawTimeListEntry( lvItem, cp );
218     mCalcPartDict.insert(lvItem, cp );
219   }
220 
221   /* Fix calculation parts */
222   m_fixParts->clear();
223   tpList = m_template->getCalcPartsList( KALKPART_FIX );
224   QListIterator<CalcPart*> fixIt( tpList );
225   while( fixIt.hasNext() ) {
226     FixCalcPart *fc = static_cast<FixCalcPart*>(fixIt.next());
227     QTreeWidgetItem *lvItem = new QTreeWidgetItem( m_fixParts );
228     drawFixListEntry( lvItem, fc );
229     mCalcPartDict.insert( lvItem, fc );
230   }
231 
232   /* Material calculation */
233   m_matParts->clear();
234   tpList = m_template->getCalcPartsList( KALKPART_MATERIAL );
235   QListIterator<CalcPart*> matIt( tpList );
236   while( matIt.hasNext() ) {
237     MaterialCalcPart *mc = static_cast<MaterialCalcPart*>(matIt.next());
238     QTreeWidgetItem *lvItem = new QTreeWidgetItem( m_matParts );
239     mCalcPartDict.insert( lvItem, mc );
240     drawMatListEntry( lvItem, mc );
241   }
242 }
243 
244 
refreshPrices()245 void FlosTemplDialog::refreshPrices()
246 {
247   if( ! m_template ) return;
248 
249   /* assemble the pricing label */
250   QString t;
251   t = i18n("Calculated Price: ");
252 
253   if( m_template->calcKind() == CatalogTemplate::ManualPrice )
254   {
255     t = i18n("Manual Price: ");
256   }
257   else if( m_template->calcKind() == CatalogTemplate::Calculation )
258   {
259     int benefit = spBenefit->value();
260     QString benefitStr = i18n("(+%1%)", benefit);
261 
262     if( benefit < 0 )
263     {
264       benefitStr = QLatin1String("<font color=\"red\">")+i18n("%1%", benefit)+QLatin1String("</font>");
265     }
266     benefitStr += i18n(": ");
267     t += benefitStr;
268   }
269   else
270   {
271     // qDebug () << "ERR: unknown calculation type!" << endl;
272   }
273   m_resPreisName->setText(t);
274   m_resPreisName->setTextFormat(Qt::RichText);
275 
276   /* set Price */
277   t = m_template->unitPrice().toString();
278   m_resultPrice->setText( t );
279   m_manualPriceVal->setValue( m_template->unitPrice().toDouble() );
280 
281   /* Price parts per calculation part */
282   Geld g( m_template->costsByCalcPart( KALKPART_TIME ));
283   m_textTimePart->setText( g.toString());
284 
285   g = m_template->costsByCalcPart( KALKPART_FIX );
286   m_textFixPart->setText( g.toString());
287 
288   g = m_template->costsByCalcPart( KALKPART_MATERIAL );
289   m_textMaterialPart->setText(g.toString());
290 
291   // Benefit
292   double b = m_template->getBenefit();
293   spBenefit->setValue( qRound(b));
294 }
295 
~FlosTemplDialog()296 FlosTemplDialog::~FlosTemplDialog( )
297 {
298   delete m_fixCalcDia;
299   delete m_timePartDialog;
300   delete m_matPartDialog;
301 }
302 
303 // Check if the template was modified in the dialog
templModified()304 bool FlosTemplDialog::templModified()
305 {
306     bool modified = false;
307 
308     QString str = m_text->toPlainText();
309     modified = str != m_template->getText();
310 
311     modified = modified || (m_unit->currentText() != m_template->unit().einheitSingular());
312 
313     modified = modified || (m_addTime->isChecked() != m_template->hasTimeslice());
314 
315     str = spBenefit->cleanText();
316     bool b;
317     int new_val = str.toInt(&b);
318     modified = modified || ( b && new_val != qRound(m_template->getBenefit()));
319 
320     // calculation kind
321     CatalogTemplate::CalculationType currCalcType = m_template->calcKind();
322     modified = modified || (currCalcType != _origCalcType);
323 
324     modified = modified || _calcPartsModified;
325 
326     return modified;
327 }
328 
accept()329 void FlosTemplDialog::accept()
330 {
331     if( m_template ) {
332         // qDebug () << "Saving template ID " << m_template->getTemplID() << endl;
333 
334         QString h;
335         h = m_text->toPlainText();
336 
337         if( h != m_template->getText() ) {
338             // qDebug () << "Template Text dirty -> update" << endl;
339             m_template->setText( h );
340         }
341 
342         h = m_unit->currentText();
343         if( h != m_template->unit().einheitSingular()) {
344             // qDebug () << "Template Einheit dirty -> update to " << h << endl;
345             m_template->setUnitId( UnitManager::self()->getUnitIDSingular(h));
346         }
347 
348         /* count time */
349         bool c = m_addTime->isChecked();
350         if( c != m_template->hasTimeslice() ) {
351             m_template->setHasTimeslice(c);
352         }
353 
354         /* benefit */
355         h = spBenefit->cleanText();
356         bool b;
357         int new_val = h.toInt(&b);
358         if( b && new_val != qRound(m_template->getBenefit())) {
359             m_template->setBenefit(new_val);
360             // qDebug () << "benefit dirty ->update to " << g << endl;
361         }
362 
363         // Calculationtype
364         int selId = m_gbPriceSrc->checkedId();
365         CatalogTemplate::CalculationType calcType = CatalogTemplate::Unknown;
366         if( selId == 0 ) {
367             calcType = CatalogTemplate::ManualPrice;
368         } else if( selId == 1 ) {
369             calcType = CatalogTemplate::Calculation;
370         } else {
371             // qDebug () << "ERROR: Calculation type not selected, id is " << selId << endl;
372         }
373         m_template->setCalculationType( calcType );
374 
375         // reread the manual price
376         double dd = m_manualPriceVal->value();
377         m_template->setManualPrice( Geld( dd ) );
378 
379         h = cbChapter->currentText();
380         // qDebug () << "catalog chapter is " << h << endl;
381 
382         _calcPartsModified = false;
383 
384         if( m_template->save() ) {
385             emit( editAccepted( m_template ) );
386             KatalogMan::self()->notifyKatalogChange( m_katalog, m_template->getTemplID() );
387         } else {
388             QMessageBox::warning(0, i18n("Template Error"), i18n("Saving of this template failed, sorry"));
389 
390         }
391     }
392     // qDebug () << "*** Saving finished " << endl;
393     QDialog::accept();
394 }
395 
reject()396 void FlosTemplDialog::reject()
397 {
398   if(confirmClose() == true) {
399     // let QDialog clean away the dialog.
400     QDialog::reject();
401   }
402 }
403 
closeEvent(QCloseEvent * event)404 void FlosTemplDialog::closeEvent ( QCloseEvent * event )
405 {
406   if(confirmClose() == false)
407     event->ignore();
408   else
409     event->accept();
410 }
411 
confirmClose()412 bool FlosTemplDialog::confirmClose()
413 {
414     if( templModified() ) {
415         QMessageBox msgBox;
416         msgBox.setText(i18n("The template has been modified."));
417         msgBox.setInformativeText(i18n("Do you want to discard your changes?"));
418         msgBox.setStandardButtons(QMessageBox::Discard | QMessageBox::Cancel);
419         msgBox.setDefaultButton(QMessageBox::Cancel);
420         int ret = msgBox.exec();
421 
422         if( ret == QMessageBox::Cancel  ) {
423             return false;
424         }
425     }
426 
427     mCalcPartDict.clear();
428 
429     m_timeParts->clear ();
430     m_fixParts->clear ();
431     m_matParts->clear ();
432 
433     m_katalog->reload(m_template->getTemplID());
434 
435     if ( m_templateIsNew ) {
436       // remove the listview item if it was created newly
437       emit editRejected();
438     }
439 
440     return true;
441 }
442 
askChapterChange(FloskelTemplate *,int)443 bool FlosTemplDialog::askChapterChange( FloskelTemplate*, int )
444 {
445     QMessageBox msgBox;
446     msgBox.setText(i18n( "The catalog chapter was changed for this template.\n"
447                          "Do you really want to move the template to the new chapter?"));
448     msgBox.setInformativeText(i18n("Chapter Change"));
449     msgBox.setStandardButtons(QMessageBox::Yes| QMessageBox::No);
450     msgBox.setDefaultButton(QMessageBox::Yes);
451     int ret = msgBox.exec();
452 
453     return( ret == QMessageBox::Yes);
454 }
455 
slManualPriceChanged(double dd)456 void FlosTemplDialog::slManualPriceChanged(double dd)
457 {
458     qDebug () << "Changing manual price:" << dd << endl;
459   if( ! m_template ) return;
460   // qDebug () << "Updating manual price!" << endl;
461   m_template->setManualPrice( Geld( dd ));
462   refreshPrices();
463 }
464 
benefitValue()465 double FlosTemplDialog::benefitValue()
466 {
467     double val = 0;
468     CalcPartList cparts = m_template->getCalcPartsList();
469 
470     // Currently still all calcparts have the same value of benefit.
471     // once this changes, this needs to be fixed accordingly.
472     if( cparts.size() > 0 ) {
473         val = cparts.at(0)->getProzentPlus();
474     }
475 
476     return val;
477 }
478 
slBenefitChange(int newBen)479 void FlosTemplDialog::slBenefitChange( int newBen )
480 {
481     int oldBen = qRound(m_template->getBenefit());
482     if( oldBen != newBen ) {
483         m_template->setBenefit(newBen);
484         refreshPrices();
485         _calcPartsModified = true;
486     }
487 }
488 
slAddFixPart()489 void FlosTemplDialog::slAddFixPart()
490 {
491   if( ! m_template ) return;
492 
493   FixCalcDialog dia(this);
494 
495   if( dia.exec() == QDialog::Accepted )
496   {
497     FixCalcPart *cp = new FixCalcPart( dia.getName(), dia.getPreis());
498     cp->setMenge( dia.getMenge());
499     cp->setProzentPlus(benefitValue());
500 
501     cp->setDirty(true);
502 
503     QTreeWidgetItem *lvItem = new QTreeWidgetItem( m_fixParts);
504     drawFixListEntry( lvItem, cp );
505     mCalcPartDict.insert( lvItem, cp );
506     m_template->addCalcPart( cp );
507     refreshPrices();
508     _calcPartsModified = true;
509   }
510 }
511 
slRemoveFixPart()512 void FlosTemplDialog::slRemoveFixPart()
513 {
514   if( ! m_template || ! m_fixParts ) return;
515 
516   QTreeWidgetItem *item = m_fixParts->currentItem();
517 
518   if( item )
519   {
520     CalcPart *cp = mCalcPartDict[item];
521     if( cp )
522     {
523       m_template->removeCalcPart(cp);
524     }
525     delete item;
526 
527     refreshPrices();
528     _calcPartsModified = true;
529   }
530 }
531 
slEditFixPart()532 void FlosTemplDialog::slEditFixPart()
533 {
534   if( ! m_template || !m_fixParts ) return;
535 
536   // qDebug () << "Edit fix part!" << endl;
537 
538   QTreeWidgetItem *item = m_fixParts->currentItem();
539 
540   if( item )
541   {
542     FixCalcPart *cp = static_cast<FixCalcPart*>(mCalcPartDict[item]);
543     if( cp )
544     {
545       m_fixCalcDia = new FixCalcDialog(this);
546       m_fixCalcDia->setCalcPart(cp);
547       connect( m_fixCalcDia, SIGNAL(fixCalcPartChanged(FixCalcPart*)),
548                this, SLOT(slFixCalcPartChanged(FixCalcPart*)));
549       m_fixCalcDia->show();
550     }
551   }
552 }
553 
slFixCalcPartChanged(FixCalcPart * cp)554 void FlosTemplDialog::slFixCalcPartChanged(FixCalcPart *cp)
555 {
556   refreshPrices();
557   _calcPartsModified = true;
558   drawFixListEntry(m_fixParts->currentItem(), cp);
559 }
560 
slTimeCalcPartChanged(TimeCalcPart * cp)561 void FlosTemplDialog::slTimeCalcPartChanged(TimeCalcPart *cp)
562 {
563   refreshPrices();
564   _calcPartsModified = true;
565   drawTimeListEntry(m_timeParts->currentItem(), cp);
566 }
567 
slAddTimePart()568 void FlosTemplDialog::slAddTimePart()
569 {
570   if( ! m_template ) return;
571   TimeCalcDialog dia(this);
572 
573   if( dia.exec() == QDialog::Accepted ) {
574     TimeCalcPart *cp = new TimeCalcPart( dia.getName(), dia.getDauer(),
575                                          TimeCalcPart::timeUnitFromString(dia.unitStr()),
576                                          0 );
577     cp->setGlobalStdSetAllowed( dia.allowGlobal());
578     StdSatz std = StdSatzMan::self()->getStdSatz( dia.getStundensatzName());
579     cp->setStundensatz( std );
580     cp->setProzentPlus(benefitValue());
581     QTreeWidgetItem *lvItem = new QTreeWidgetItem( m_timeParts);
582     drawTimeListEntry( lvItem, cp );
583     mCalcPartDict.insert( lvItem, cp );
584     m_template->addCalcPart( cp );
585     refreshPrices();
586     _calcPartsModified = true;
587   }
588 }
589 
590 /*
591  * stellt einen TimeCalcPart als ListViewItem dar. Wird gebraucht, wenn das
592  * item neu ist, aber auch beim Editieren
593  */
drawTimeListEntry(QTreeWidgetItem * it,TimeCalcPart * cp)594 void FlosTemplDialog::drawTimeListEntry( QTreeWidgetItem *it, TimeCalcPart *cp )
595 {
596 
597   if( !( it && cp) )
598     return;
599 
600   it->setText( 0, cp->getName());
601   it->setText( 1, QString::number(cp->duration())
602                + QLatin1Char(' ') + TimeCalcPart::timeUnitString(cp->timeUnit()));
603   it->setText( 2, cp->getStundensatz().getName());
604   it->setText( 3, cp->globalStdSetAllowed() ? i18n("Yes") : i18n("No"));
605 }
606 
drawFixListEntry(QTreeWidgetItem * it,FixCalcPart * cp)607 void FlosTemplDialog::drawFixListEntry( QTreeWidgetItem* it, FixCalcPart *cp )
608 {
609   if( !( it && cp) )
610     return;
611 
612   it->setText( 0, DefaultProvider::self()->locale()->toString(cp->getMenge()));
613   it->setText( 1, cp->getName());
614   it->setText( 2, cp->unitPreis().toString());
615   it->setText( 3, cp->basisKosten().toString());
616 }
617 
drawMatListEntry(QTreeWidgetItem * it,MaterialCalcPart * mc)618 void FlosTemplDialog::drawMatListEntry( QTreeWidgetItem *it, MaterialCalcPart *mc )
619 {
620   it->setText( 0, mc->getName());
621   it->setText( 1, QString::number(mc->getCalcAmount(), 'f',2));
622   it->setText( 2, mc->getMaterial()->unit().einheitSingular());
623   it->setText( 3, mc->basisKosten().toString());
624   it->setText( 4, QString::number(mc->getMaterial()->getAmountPerPack(), 'f',2));
625   it->setText( 5, mc->getMaterial()->salesPrice().toString());
626 }
627 
628 
slRemoveTimePart()629 void FlosTemplDialog::slRemoveTimePart()
630 {
631   if( ! m_template || !m_timeParts ) return;
632 
633   QTreeWidgetItem *item = m_timeParts->currentItem();
634 
635   if( item )
636   {
637     CalcPart *cp = mCalcPartDict[item];
638     if( cp )
639     {
640       m_template->removeCalcPart(cp);
641     }
642     delete item;
643 
644     refreshPrices();
645     _calcPartsModified = true;
646   }
647 }
648 
slEditTimePart()649 void FlosTemplDialog::slEditTimePart()
650 {
651   if( ! m_template || !m_timeParts ) return;
652 
653   // qDebug () << "Edit time part!" << endl;
654 
655   QTreeWidgetItem *item = m_timeParts->currentItem();
656 
657   if( item ) {
658     TimeCalcPart *cp = static_cast<TimeCalcPart*>(mCalcPartDict[item]);
659     if( cp ) {
660       m_timePartDialog = new TimeCalcDialog(this);
661       m_timePartDialog->setTimeCalcPart(cp);
662       connect( m_timePartDialog, SIGNAL(timeCalcPartChanged(TimeCalcPart*)),
663                this, SLOT(slTimeCalcPartChanged(TimeCalcPart*)) );
664       m_timePartDialog->show();
665     } else {
666       // qDebug () << "No time calc part found for this item" << endl;
667     }
668   } else {
669     // qDebug () << "No current Item!";
670   }
671   refreshPrices();
672   _calcPartsModified = true;
673 }
674 
675 /*
676  * Achtung: slAddMatPart fgt keinen neuen kompletten Materialpart
677  * hinzu, sondern nur ein neues Material zu dem einen, bestehenden
678  * Materialpart.
679  */
slAddMatPart()680 void FlosTemplDialog::slAddMatPart()
681 {
682   if( ! m_template ) return;
683 
684   MaterialSelectDialog dia( this );
685   connect( &dia, SIGNAL( materialSelected( int, double ) ),
686            this, SLOT( slNewMaterial( int, double ) ) );
687   dia.exec();
688 }
689 
690 /*
691  * Slot, der aufgerufen wird, wenn im Materialeditor ein Material zur Kalkulation
692  * geschickt wird.
693  */
slNewMaterial(int matID,double amount)694 void FlosTemplDialog::slNewMaterial( int matID, double amount )
695 {
696     // qDebug () << "Material ID: " << matID << endl;
697 
698     // TODO: Checken, ob der richtige Tab aktiv ist.
699     // TODO: Check if the material is already in the calcpart (is this really needed??)
700     MaterialCalcPart *mc = new MaterialCalcPart(matID, 0, amount);
701     if( mc && ! mc->getMaterial() ) {
702         // the material is still unknown to the catalog because it was just entered
703         // in the material catalog
704         qDebug() << "ERR: MaterialCalcPart without Material!";
705         return;
706     }
707 
708     if( mc ) {
709         mc->setProzentPlus(benefitValue());
710 
711         m_template->addCalcPart( mc );
712         QTreeWidgetItem *lvItem = new QTreeWidgetItem(m_matParts);
713         drawMatListEntry( lvItem, mc );
714         mCalcPartDict.insert( lvItem, mc );
715         refreshPrices();
716         _calcPartsModified = true;
717     }
718 }
719 
720 
slEditMatPart()721 void FlosTemplDialog::slEditMatPart()
722 {
723   if( ! m_template || !m_matParts ) return;
724 
725   // qDebug () << "Edit Material part!" << endl;
726 
727   QTreeWidgetItem *item = m_matParts->currentItem();
728 
729   MaterialCalcPart *mc = static_cast<MaterialCalcPart*>( mCalcPartDict[item] );
730 
731   if( mc ) {
732       m_matPartDialog = new MatCalcDialog( mc, this);
733 
734       connect( m_matPartDialog, SIGNAL(matCalcPartChanged(MaterialCalcPart*)),
735                this, SLOT(slMatCalcPartChanged(MaterialCalcPart*)));
736       m_matPartDialog->setModal(true);
737       m_matPartDialog->show();
738     } else {
739       // qDebug () << "No such MaterialCalcPart!";
740     }
741 }
742 
slMatCalcPartChanged(MaterialCalcPart * mc)743 void FlosTemplDialog::slMatCalcPartChanged(MaterialCalcPart *mc)
744 {
745   drawMatListEntry(m_matParts->currentItem(), mc);
746   refreshPrices();
747   _calcPartsModified = true;
748 }
749 
slRemoveMatPart()750 void FlosTemplDialog::slRemoveMatPart()
751 {
752   if( ! m_template || ! m_matParts ) return;
753 
754   QTreeWidgetItem *item = m_matParts->currentItem();
755 
756   if( item )
757   {
758     CalcPart *cp = mCalcPartDict[item];
759     if( cp )
760     {
761       m_template->removeCalcPart(cp);
762     }
763     delete item;
764 
765     refreshPrices();
766     _calcPartsModified = true;
767   }
768 }
769 
770 /*
771  * Slot for managing the switch from manual to calculated price
772  * and vice versa
773  */
slCalcOrFix(int button)774 void FlosTemplDialog::slCalcOrFix(int button)
775 {
776   bool ok = true;
777   bool manualEnabled = true;
778 
779   if( button == 0 ) {
780     /* switched to manual price */
781     if( m_template ) {
782         m_template->setCalculationType( CatalogTemplate::ManualPrice );
783     }
784   } else if( button == 1 ) {
785     /* switched to calculated */
786     if( m_template ) {
787       m_template->setCalculationType( CatalogTemplate::Calculation );
788     }
789     manualEnabled = false;
790   } else {
791     /* unknown knob*/
792     ok = false;
793   }
794 
795   if( ok ) {
796       m_manualPriceVal->setEnabled( manualEnabled );
797 
798       m_textTimePart->setEnabled(!manualEnabled);
799       m_textFixPart->setEnabled(!manualEnabled);
800       m_textMaterialPart->setEnabled(!manualEnabled);
801 
802       m_tLabelMat->setEnabled(!manualEnabled);
803       m_tLabelFix->setEnabled(!manualEnabled);
804       m_tLabelTime->setEnabled(!manualEnabled);
805       m_tLabelProfit->setEnabled(!manualEnabled);
806       spBenefit->setEnabled(!manualEnabled);
807 
808       refreshPrices();
809   }
810 }
811 
812 
slSetNewText()813 void FlosTemplDialog::slSetNewText()
814 {
815     QPushButton *okButton = _buttonBox->button(QDialogButtonBox::Ok);
816 
817     if( ! m_text || m_text->toPlainText().isEmpty() ) {
818         okButton->setEnabled(false);
819     } else {
820         okButton->setEnabled(true);
821     }
822 
823     if( m_text ) {
824         const QString t = Portal::textWrap( m_text->toPlainText(), 80, 5 );
825         const QStringList li = t.split(QLatin1Char('\n'));
826         QString longest;
827         for( const QString& p : li ) {
828             if( p.length() > longest.length() )
829                 longest = p;
830         }
831         QFontMetrics fm(m_textDispFix->font());
832         int w = 10+fm.width(longest);
833 
834         if( m_textDispTime) {
835             m_textDispTime->setText(t);
836             m_textDispTime->setMinimumWidth(w);
837         }
838         if( m_textDispFix) {
839             m_textDispFix->setText(t);
840             m_textDispFix->setMinimumWidth(w);
841         }
842         if( m_textDispMat) {
843             m_textDispMat->setText(t);
844             m_textDispMat->setMinimumWidth(w);
845         }
846     }
847 }
848 /* END */
849