1 /*****************************************************************************************
2 begin : Sat Apr 26 2003
3 copyright : (C) 2003 by Jeroen Wijnhout (wijnhout@science.uva.nl)
4 2005 by Holger Danielsson (holger.danielsson@t-online.de)
5 2006-2014 by Michel Ludwig (michel.ludwig@kdemail.net)
6 ******************************************************************************************/
7
8 /***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17 #include "dialogs/newfilewizard.h"
18
19 #include <QComboBox>
20 #include <QDir>
21 #include <QFileInfo>
22 #include <QLabel>
23 #include <QLayout>
24 #include <QMap>
25
26 #include <KConfig>
27 #include <KIconLoader>
28 #include <KLocalizedString>
29 #include <KMessageBox>
30 #include <KConfigGroup>
31 #include <KSharedConfig>
32 #include <QDialogButtonBox>
33 #include <QPushButton>
34 #include <QVBoxLayout>
35
36 #include "kiledebug.h"
37 #include "widgets/newdocumentwidget.h"
38
39 #define LATEX_TYPE 0
40 #define BIBTEX_TYPE 1
41 #define SCRIPT_TYPE 2
42
NewFileWizard(KileTemplate::Manager * templateManager,KileDocument::Type startType,QWidget * parent,const char * name)43 NewFileWizard::NewFileWizard(KileTemplate::Manager *templateManager, KileDocument::Type startType,
44 QWidget *parent, const char *name)
45 : QDialog(parent), m_templateManager(templateManager), m_currentlyDisplayedType(-1)
46 {
47 setObjectName(name);
48 setWindowTitle(i18n("New File"));
49 setModal(true);
50
51 QVBoxLayout *mainLayout = new QVBoxLayout;
52 setLayout(mainLayout);
53
54 // first read config
55 KConfigGroup newFileWizardGroup = KSharedConfig::openConfig()->group("NewFileWizard");
56 bool wizard = newFileWizardGroup.readEntry("UseWizardWhenCreatingEmptyFile", false);
57 int w = newFileWizardGroup.readEntry("width", -1);
58 if(w == -1) {
59 w = width();
60 }
61 int h = newFileWizardGroup.readEntry("height", -1);
62 if(h == -1) {
63 h = height();
64 }
65
66 m_newDocumentWidget = new NewDocumentWidget(this);
67 connect(m_newDocumentWidget->templateIconView, SIGNAL(itemDoubleClicked(QListWidgetItem*)), SLOT(okButtonClicked()));
68 m_templateManager->scanForTemplates();
69 m_newDocumentWidget->templateIconView->setTemplateManager(m_templateManager);
70
71 connect(m_newDocumentWidget->documentTypeComboBox, SIGNAL(activated(int)), this, SLOT(slotActivated(int)));
72 connect(m_newDocumentWidget->templateIconView, SIGNAL(classFileSearchFinished()), this, SLOT(restoreSelectedIcon()));
73
74 mainLayout->addWidget(m_newDocumentWidget);
75
76 m_newDocumentWidget->documentTypeComboBox->insertItem(LATEX_TYPE, i18n("LaTeX Document"));
77 m_newDocumentWidget->documentTypeComboBox->insertItem(BIBTEX_TYPE, i18n("BibTeX Document"));
78 m_newDocumentWidget->documentTypeComboBox->insertItem(SCRIPT_TYPE, i18n("Kile Script"));
79
80 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
81 QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
82 okButton->setDefault(true);
83 okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
84 connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
85 connect(buttonBox, SIGNAL(accepted()), this, SLOT(okButtonClicked()));
86 connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
87 mainLayout->addWidget(buttonBox);
88
89 // set config entries
90 m_newDocumentWidget->quickStartWizardCheckBox->setChecked(wizard);
91 resize(w,h);
92
93 int index = 0;
94 switch(startType) {
95 default: // fall through
96 case KileDocument::LaTeX:
97 index = LATEX_TYPE;
98 break;
99 case KileDocument::BibTeX:
100 index = BIBTEX_TYPE;
101 break;
102 case KileDocument::Script:
103 index = SCRIPT_TYPE;
104 break;
105 }
106
107 // select the document type
108 m_newDocumentWidget->documentTypeComboBox->setCurrentIndex(index);
109 m_currentlyDisplayedType = index;
110 displayType(index);
111 }
112
~NewFileWizard()113 NewFileWizard::~NewFileWizard()
114 {
115 }
116
getSelection() const117 TemplateItem* NewFileWizard::getSelection() const
118 {
119 QList<QListWidgetItem*> selectedItems = m_newDocumentWidget->templateIconView->selectedItems();
120 if(selectedItems.isEmpty()) {
121 return Q_NULLPTR;
122 }
123 return static_cast<TemplateItem*>(selectedItems.first());
124 }
125
useWizard()126 bool NewFileWizard::useWizard()
127 {
128 // check (among other things) whether we want to create a LaTeX document
129 return ((m_newDocumentWidget->documentTypeComboBox->currentIndex() == 0)
130 && getSelection()
131 && (getSelection()->name() == KileTemplate::Manager::defaultEmptyTemplateCaption()
132 || getSelection()->name() == KileTemplate::Manager::defaultEmptyLaTeXTemplateCaption())
133 && m_newDocumentWidget->quickStartWizardCheckBox->isChecked());
134 }
135
getConfigKey(int index)136 QString NewFileWizard::getConfigKey(int index)
137 {
138 QString configKey = "NewFileWizardSelectedIcon";
139 switch(index) {
140 case LATEX_TYPE:
141 configKey += "LaTeX";
142 break;
143
144 case BIBTEX_TYPE:
145 configKey += "BibTeX";
146 break;
147
148 case SCRIPT_TYPE:
149 configKey += "Script";
150 break;
151 }
152 return configKey;
153 }
154
storeSelectedIcon()155 void NewFileWizard::storeSelectedIcon()
156 {
157 if(m_currentlyDisplayedType < 0) {
158 return;
159 }
160 TemplateItem *selectedItem = getSelection();
161 if (selectedItem) {
162 KSharedConfig::openConfig()->group("default").writeEntry(getConfigKey(m_currentlyDisplayedType), selectedItem->name());
163 }
164 }
165
restoreSelectedIcon()166 void NewFileWizard::restoreSelectedIcon()
167 {
168 KConfigGroup defaultGroup = KSharedConfig::openConfig()->group("default");
169 QString selectedIconName = defaultGroup.readEntry(getConfigKey(m_currentlyDisplayedType),
170 KileTemplate::Manager::defaultEmptyTemplateCaption());
171 QList<QListWidgetItem*> items = m_newDocumentWidget->templateIconView->findItems(selectedIconName, Qt::MatchExactly);
172 if(items.count() > 0) {
173 items[0]->setSelected(true);
174 }
175 }
176
okButtonClicked()177 void NewFileWizard::okButtonClicked()
178 {
179 KConfigGroup newFileWizardGroup = KSharedConfig::openConfig()->group("NewFileWizard");
180
181 newFileWizardGroup.writeEntry("UseWizardWhenCreatingEmptyFile", m_newDocumentWidget->quickStartWizardCheckBox->isChecked());
182 newFileWizardGroup.writeEntry("width", width());
183 newFileWizardGroup.writeEntry("height", height());
184
185 storeSelectedIcon();
186 accept();
187 }
188
slotActivated(int index)189 void NewFileWizard::slotActivated(int index)
190 {
191 storeSelectedIcon();
192 m_currentlyDisplayedType = index;
193 displayType(index);
194 }
195
displayType(int index)196 void NewFileWizard::displayType(int index)
197 {
198 switch(index) {
199 case LATEX_TYPE:
200 m_newDocumentWidget->templateIconView->fillWithTemplates(KileDocument::LaTeX);
201 break;
202
203 case BIBTEX_TYPE:
204 m_newDocumentWidget->templateIconView->fillWithTemplates(KileDocument::BibTeX);
205 break;
206
207 case SCRIPT_TYPE:
208 m_newDocumentWidget->templateIconView->fillWithTemplates(KileDocument::Script);
209 break;
210 }
211 m_newDocumentWidget->quickStartWizardCheckBox->setEnabled((index == 0));
212
213 // and select an icon
214 restoreSelectedIcon();
215 }
216
217