1 /***************************************************************************
2  inserttemplatedialog.cpp  - small dialog to insert templates into documents
3                              -------------------
4     begin                : Sep 2006
5     copyright            : (C) 2006 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 "inserttempldialog.h"
19 
20 // include files for Qt
21 #include <QTextEdit>
22 #include <QLabel>
23 #include <QComboBox>
24 #include <QCheckBox>
25 #include <QVBoxLayout>
26 #include <QToolTip>
27 #include <QMap>
28 #include <QDate>
29 #include <QDebug>
30 #include <QLocale>
31 
32 #include <unistd.h>
33 #include <sys/types.h>
34 #include <pwd.h>
35 
36 #include "ui_inserttmplbase.h"
37 #include "templtopositiondialogbase.h"
38 #include "katalog.h"
39 #include "einheit.h"
40 #include "unitmanager.h"
41 #include "defaultprovider.h"
42 #include "kraftsettings.h"
43 #include "tagman.h"
44 
InsertTemplDialog(QWidget * parent)45 InsertTemplDialog::InsertTemplDialog( QWidget *parent )
46   : TemplToPositionDialogBase( parent )
47 {
48   QWidget *w = new QWidget( this );
49 
50   mBaseWidget = new Ui::insertTmplBase;
51   mBaseWidget->setupUi( w );
52   mBaseWidget->dmUnitCombo->insertItems( -1, UnitManager::self()->allUnits() );
53 
54   mBaseWidget->mPriceVal->setSuffix( DefaultProvider::self()->currencySymbol() );
55 
56   mBaseWidget->mPriceVal->setMinimum( 0 );
57   mBaseWidget->mPriceVal->setMaximum( 100000 );
58   mBaseWidget->mPriceVal->setDecimals( 2 );
59   mBaseWidget->dmAmount->setDecimals( 2 );
60   mBaseWidget->dmAmount->setRange( 0, 100000 );
61   mBaseWidget->dmAmount->setSingleStep( 1 );
62   // mBaseWidget->dmAmount->setSteps( 1, 10 );
63 
64   // hide the chapter combo by default
65   mBaseWidget->mKeepGroup->hide();
66 
67   // Fill the tags list
68   QGroupBox *group = mBaseWidget->mTagGroup;
69   QVBoxLayout *groupLay = new QVBoxLayout;
70   group->setLayout( groupLay );
71 
72   QStringList tags = TagTemplateMan::self()->allTagTemplates();
73 
74   int cnt = 0;
75   for ( QStringList::Iterator it = tags.begin(); it != tags.end(); ++it ) {
76     QCheckBox *cb = new QCheckBox( *it );
77     QString desc = TagTemplateMan::self()->getTagTemplate( *it ).description();
78     // QToolTip::add( cb, desc );
79     groupLay->insertWidget( cnt++, cb );
80     mTagMap[cb] = *it;
81   }
82   groupLay->addStretch();
83 
84   QVBoxLayout *lay = new QVBoxLayout(this);
85   lay->setMargin(0);
86   lay->addWidget(w);
87   setLayout(lay);
88 
89   connect(mBaseWidget->mButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
90   connect(mBaseWidget->mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
91 }
92 
setDocPosition(DocPosition * dp,bool isNew,bool showPrices)93 void InsertTemplDialog::setDocPosition(DocPosition *dp, bool isNew , bool showPrices)
94 {
95   if ( dp ) {
96     mParkPosition = *dp;
97 
98     mBaseWidget->dmTextEdit->setText( prepareText(mParkPosition.text()) );
99 
100     mBaseWidget->dmAmount->setValue( mParkPosition.amount() );
101     mBaseWidget->dmUnitCombo->setCurrentIndex(mBaseWidget->dmUnitCombo->findText( mParkPosition.unit().einheit( 1.0 )));
102     mBaseWidget->mPriceVal->setValue( mParkPosition.unitPrice().toDouble() );
103 
104     if ( mParkPosition.text().isEmpty() ) {
105       mBaseWidget->dmHeaderText->setText( i18n( "Create a new Item" ) );
106     } else {
107       mBaseWidget->dmHeaderText->setText( i18n( "Create a new Item from Template" ) );
108     }
109     if ( isNew ) {
110       mBaseWidget->dmTextEdit->setFocus();
111     } else {
112       mBaseWidget->dmAmount->setFocus();
113     }
114     mBaseWidget->mPriceVal->setVisible(showPrices);
115     mBaseWidget->priceBoxTextLabel->setVisible(showPrices);
116   }
117 }
118 
119 #define QL1(x) QLatin1String(x)
prepareText(const QString & input)120 QString InsertTemplDialog::prepareText( const QString& input )
121 {
122     QString in(input);
123 
124     QLocale *locale = DefaultProvider::self()->locale();
125     QString dateStr = locale->toString( QDate::currentDate() );
126     in.replace(QL1("{{DATE}}"), dateStr, Qt::CaseInsensitive);
127 
128     QString timeStr = locale->toString(QTime::currentTime());
129     in.replace(QL1("{{TIME}}"), timeStr, Qt::CaseInsensitive);
130 
131     if( in.contains(QL1("{{USERNAME}}"))) {
132         register struct passwd *pw;
133         register uid_t uid;
134         uid = geteuid ();
135         pw = getpwuid (uid);
136         if (pw) {
137             in.replace(QL1("{{USERNAME}}"), QString::fromUtf8(pw->pw_gecos), Qt::CaseInsensitive);
138         }
139     }
140 
141     return in;
142 }
143 
144 
getPositionCombo()145 QComboBox *InsertTemplDialog::getPositionCombo()
146 {
147   return mBaseWidget->dmPositionCombo;
148 }
149 
docPosition()150 DocPosition InsertTemplDialog::docPosition()
151 {
152   mParkPosition.setText( mBaseWidget->dmTextEdit->toPlainText() );
153   mParkPosition.setAmount( mBaseWidget->dmAmount->value() );
154   mParkPosition.setUnitPrice( Geld( mBaseWidget->mPriceVal->value() ) );
155   int uid = UnitManager::self()->getUnitIDSingular( mBaseWidget->dmUnitCombo->currentText() );
156 
157   mParkPosition.setUnit( UnitManager::self()->getUnit( uid ) );
158   // mParkPosition.setPosition( itemPos );
159 
160   QMapIterator<QCheckBox*, QString> i(mTagMap);
161   while (i.hasNext()) {
162     i.next();
163     if( i.key()->isChecked() ) {
164       mParkPosition.setTag( i.value() );
165     }
166   }
167 
168   // qDebug () << "in the dialog: " << mParkPosition.tags() << endl;
169   return mParkPosition;
170 }
171 
172 
~InsertTemplDialog()173 InsertTemplDialog::~InsertTemplDialog()
174 {
175   QString c = mBaseWidget->mComboChapter->currentText();
176   if ( ! c.isEmpty() ) {
177     KraftSettings::self()->setInsertTemplChapterName( c );
178     KraftSettings::self()->save();
179   }
180 }
181 
setCatalogChapters(const QList<CatalogChapter> & chapters,const QString & selectedChap)182 void InsertTemplDialog::setCatalogChapters( const QList<CatalogChapter>& chapters, const QString& selectedChap)
183 {
184     if ( chapters.count() > 0 ) {
185         QStringList chapterNames;
186         for( CatalogChapter chapter: chapters ) {
187             if (!chapter.name().isEmpty())
188                 chapterNames.append( chapter.name() );
189         }
190         mBaseWidget->mKeepGroup->show();
191         mBaseWidget->mComboChapter->insertItems( -1, chapterNames );
192 
193         QString selChap { selectedChap };
194         if (!selectedChap.isEmpty())  {
195             mBaseWidget->mKeepGroup->setChecked(true);
196         } else {
197             selChap = KraftSettings::self()->insertTemplChapterName();
198         }
199         if (!selChap.isEmpty() && chapterNames.contains(selChap))
200             mBaseWidget->mComboChapter->setCurrentIndex(mBaseWidget->mComboChapter->findText(selChap));
201     }
202 }
203 
204 // return only a chapter if the checkbox is checked.
chapter() const205 QString InsertTemplDialog::chapter() const
206 {
207   QString re;
208   if ( mBaseWidget->mKeepGroup->isChecked() )
209     re = mBaseWidget->mComboChapter->currentText();
210   return re;
211 }
212 
213 /* END */
214 
215