|
16 | 16 |
|
17 | 17 | #include "qgscomposermanager.h" |
18 | 18 | #include "qgisapp.h" |
| 19 | +#include "qgsapplication.h" |
19 | 20 | #include "qgscomposer.h" |
| 21 | +#include "qgslogger.h" |
| 22 | +#include <QDir> |
20 | 23 | #include <QInputDialog> |
21 | 24 | #include <QListWidgetItem> |
22 | 25 | #include <QMessageBox> |
@@ -48,6 +51,26 @@ void QgsComposerManager::initialize() |
48 | 51 | item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable ); |
49 | 52 | mItemComposerMap.insert( item, *it ); |
50 | 53 | } |
| 54 | + |
| 55 | + //search for default templates in $pkgDataPath/composer_templates |
| 56 | + QDir defaultTemplateDir( QgsApplication::pkgDataPath() + "/composer_templates" ); |
| 57 | + if ( !defaultTemplateDir.exists() ) |
| 58 | + { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + QFileInfoList defaultTemplateFiles = defaultTemplateDir.entryInfoList( QDir::Files ); |
| 63 | + QFileInfoList::const_iterator fileIt = defaultTemplateFiles.constBegin(); |
| 64 | + |
| 65 | + for ( ; fileIt != defaultTemplateFiles.constEnd(); ++fileIt ) |
| 66 | + { |
| 67 | + mDefaultTemplateMap.insert( fileIt->baseName(), fileIt->absoluteFilePath() ); |
| 68 | + if ( mComposerListWidget->findItems( fileIt->baseName(), Qt::MatchExactly ).size() < 1 ) |
| 69 | + { |
| 70 | + QListWidgetItem* item = new QListWidgetItem( fileIt->baseName(), mComposerListWidget ); |
| 71 | + mItemComposerMap.insert( item, 0 ); |
| 72 | + } |
| 73 | + } |
51 | 74 | } |
52 | 75 |
|
53 | 76 | void QgsComposerManager::on_mAddButton_clicked() |
@@ -110,13 +133,48 @@ void QgsComposerManager::on_mShowPushButton_clicked() |
110 | 133 | return; |
111 | 134 | } |
112 | 135 |
|
113 | | - //delete composer |
114 | 136 | QMap<QListWidgetItem*, QgsComposer*>::iterator it = mItemComposerMap.find( item ); |
115 | 137 | if ( it != mItemComposerMap.end() ) |
116 | 138 | { |
117 | | - it.value()->show(); |
118 | | - it.value()->activate(); |
119 | | - it.value()->stackUnder( this ); |
| 139 | + QgsComposer* c = 0; |
| 140 | + if ( it.value() ) //a normal composer |
| 141 | + { |
| 142 | + c = it.value(); |
| 143 | + it.value()->show(); |
| 144 | + } |
| 145 | + else //create composer from default template |
| 146 | + { |
| 147 | + QMap<QString, QString>::const_iterator templateIt = mDefaultTemplateMap.find( it.key()->text() ); |
| 148 | + if ( templateIt == mDefaultTemplateMap.constEnd() ) |
| 149 | + { |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + QDomDocument templateDoc; |
| 154 | + QFile templateFile( templateIt.value() ); |
| 155 | + if ( !templateFile.open( QIODevice::ReadOnly ) ) |
| 156 | + { |
| 157 | + return; |
| 158 | + } |
| 159 | + |
| 160 | + if ( !templateDoc.setContent( &templateFile, false ) ) |
| 161 | + { |
| 162 | + return; |
| 163 | + } |
| 164 | + c = mQgisApp->createNewComposer(); |
| 165 | + c->setTitle( it.key()->text() ); |
| 166 | + if ( c ) |
| 167 | + { |
| 168 | + c->readXML( templateDoc ); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + if ( c ) |
| 173 | + { |
| 174 | + c->show(); |
| 175 | + c->activate(); |
| 176 | + c->stackUnder( this ); |
| 177 | + } |
120 | 178 | } |
121 | 179 | } |
122 | 180 |
|
@@ -157,3 +215,19 @@ void QgsComposerManager::on_mComposerListWidget_itemChanged( QListWidgetItem * i |
157 | 215 | it.value()->setTitle( item->text() ); |
158 | 216 | } |
159 | 217 | } |
| 218 | + |
| 219 | +void QgsComposerManager::on_mComposerListWidget_currentItemChanged( QListWidgetItem* current, QListWidgetItem* previous ) |
| 220 | +{ |
| 221 | + if ( !current ) |
| 222 | + { |
| 223 | + return; |
| 224 | + } |
| 225 | + if ( mDefaultTemplateMap.contains( current->text() ) ) |
| 226 | + { |
| 227 | + mRenamePushButton->setEnabled( false ); |
| 228 | + } |
| 229 | + else |
| 230 | + { |
| 231 | + mRenamePushButton->setEnabled( true ); |
| 232 | + } |
| 233 | +} |
0 commit comments