|
| 1 | +/******************************************************************* |
| 2 | + qgsgrassbrowser.cpp |
| 3 | + ------------------- |
| 4 | + begin : February, 2006 |
| 5 | + copyright : (C) 2006 by Radim Blazek |
| 6 | + email : radim.blazek@gmail.com |
| 7 | +********************************************************************/ |
| 8 | +/******************************************************************** |
| 9 | + This program is free software; you can redistribute it and/or modify |
| 10 | + it under the terms of the GNU General Public License as published by |
| 11 | + the Free Software Foundation; either version 2 of the License, or |
| 12 | + (at your option) any later version. |
| 13 | +*******************************************************************/ |
| 14 | +#include <iostream> |
| 15 | +#include <vector> |
| 16 | + |
| 17 | +#include <QApplication> |
| 18 | +#include <QStyle> |
| 19 | +#include <qdir.h> |
| 20 | +#include <qfile.h> |
| 21 | +#include <qsettings.h> |
| 22 | +#include <qstringlist.h> |
| 23 | +#include <qmessagebox.h> |
| 24 | +#include <qpainter.h> |
| 25 | +#include <qpixmap.h> |
| 26 | +#include <qnamespace.h> |
| 27 | +#include <qevent.h> |
| 28 | +#include <qsize.h> |
| 29 | +#include <qicon.h> |
| 30 | +#include <QTreeView> |
| 31 | +#include <QHeaderView> |
| 32 | +#include <QMainWindow> |
| 33 | +#include <QActionGroup> |
| 34 | +#include <QToolBar> |
| 35 | +#include <QAction> |
| 36 | +#include <QTextBrowser> |
| 37 | +#include <QSplitter> |
| 38 | + |
| 39 | +#include "qgis.h" |
| 40 | +#include "qgsapplication.h" |
| 41 | + |
| 42 | +extern "C" { |
| 43 | +#include <grass/gis.h> |
| 44 | +#include <grass/Vect.h> |
| 45 | +} |
| 46 | + |
| 47 | +#include "../../src/providers/grass/qgsgrass.h" |
| 48 | +#include "qgsgrassmodel.h" |
| 49 | +#include "qgsgrassbrowser.h" |
| 50 | + |
| 51 | +QgsGrassBrowser::QgsGrassBrowser ( QgisIface *iface, |
| 52 | + QWidget * parent, Qt::WFlags f ) |
| 53 | + :mIface(iface), QMainWindow(parent, Qt::WType_Dialog) |
| 54 | +{ |
| 55 | + #ifdef QGISDEBUG |
| 56 | + std::cerr << "QgsGrassBrowser()" << std::endl; |
| 57 | + #endif |
| 58 | + |
| 59 | + QActionGroup *ag = new QActionGroup ( this ); |
| 60 | + QToolBar *tb = addToolBar(tr("Tools")); |
| 61 | + |
| 62 | + QString myIconPath = QgsApplication::themePath() + "/grass/"; |
| 63 | + mActionAddMap = new QAction( |
| 64 | + QIcon(myIconPath+"grass_add_map.png"), |
| 65 | + tr("Add selected map to canvas"), this); |
| 66 | + mActionAddMap->setEnabled(false); |
| 67 | + ag->addAction ( mActionAddMap ); |
| 68 | + tb->addAction ( mActionAddMap ); |
| 69 | + connect ( mActionAddMap, SIGNAL(triggered()), this, SLOT(addMap()) ); |
| 70 | + |
| 71 | + mActionRefresh = new QAction( |
| 72 | + QIcon(myIconPath+"grass_refresh.png"), |
| 73 | + tr("Refresh"), this); |
| 74 | + ag->addAction ( mActionRefresh ); |
| 75 | + tb->addAction ( mActionRefresh ); |
| 76 | + connect ( mActionRefresh, SIGNAL(triggered()), this, SLOT(refresh()) ); |
| 77 | + |
| 78 | + // Add model |
| 79 | + mModel = new QgsGrassModel ( this ); |
| 80 | + |
| 81 | + mTree = new QTreeView(0); |
| 82 | + mTree->header()->hide(); |
| 83 | + mTree->setModel(mModel); |
| 84 | + |
| 85 | + mTextBrowser = new QTextBrowser(0); |
| 86 | + mTextBrowser->setTextFormat(Qt::RichText); |
| 87 | + mTextBrowser->setReadOnly(TRUE); |
| 88 | + |
| 89 | + mSplitter = new QSplitter(0); |
| 90 | + mSplitter->addWidget(mTree); |
| 91 | + mSplitter->addWidget(mTextBrowser); |
| 92 | + |
| 93 | + this->setCentralWidget(mSplitter); |
| 94 | + |
| 95 | + connect ( mTree->selectionModel(), |
| 96 | + SIGNAL(selectionChanged(QItemSelection,QItemSelection)), |
| 97 | + this, SLOT(selectionChanged(QItemSelection,QItemSelection)) ); |
| 98 | + |
| 99 | + connect ( mTree->selectionModel(), |
| 100 | + SIGNAL(currentChanged(QModelIndex,QModelIndex)), |
| 101 | + this, SLOT(currentChanged(QModelIndex,QModelIndex)) ); |
| 102 | +} |
| 103 | + |
| 104 | +QgsGrassBrowser::~QgsGrassBrowser() { } |
| 105 | + |
| 106 | +void QgsGrassBrowser::refresh() |
| 107 | +{ |
| 108 | + #ifdef QGISDEBUG |
| 109 | + std::cerr << "QgsGrassBrowser::refresh()" << std::endl; |
| 110 | + #endif |
| 111 | + |
| 112 | + mModel->refresh(); |
| 113 | + mTree->update(); |
| 114 | +} |
| 115 | + |
| 116 | +void QgsGrassBrowser::addMap() |
| 117 | +{ |
| 118 | + #ifdef QGISDEBUG |
| 119 | + std::cerr << "QgsGrassBrowser::addMap()" << std::endl; |
| 120 | + #endif |
| 121 | + |
| 122 | + QModelIndexList indexes = mTree->selectionModel()->selectedIndexes(); |
| 123 | + bool mapSelected = false; |
| 124 | + |
| 125 | + QList<QModelIndex>::const_iterator it = indexes.begin(); |
| 126 | + for (; it != indexes.end(); ++it) |
| 127 | + { |
| 128 | + int type = mModel->itemType(*it); |
| 129 | + QString uri = mModel->uri(*it); |
| 130 | + if ( type == QgsGrassModel::Raster ) |
| 131 | + { |
| 132 | + std::cerr << "add raster: " << uri.ascii() << std::endl; |
| 133 | + mIface->addRasterLayer( uri ); |
| 134 | + mapSelected = true; |
| 135 | + } |
| 136 | + else if ( type == QgsGrassModel::VectorLayer ) |
| 137 | + { |
| 138 | + // TODO: common method for vector names |
| 139 | + QStringList split = QStringList::split ( '/', uri ); |
| 140 | + QString layer = split.last(); |
| 141 | + split.pop_back(); // map |
| 142 | + QString vector = split.last(); |
| 143 | + mIface->addVectorLayer( uri, layer, "grass"); |
| 144 | + std::cerr << "add vector: " << uri.ascii() << std::endl; |
| 145 | + mapSelected = true; |
| 146 | + } |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +void QgsGrassBrowser::selectionChanged(const QItemSelection & selected, const QItemSelection & deselected) |
| 151 | +{ |
| 152 | + #ifdef QGISDEBUG |
| 153 | + std::cerr << "QgsGrassBrowser::selectionChanged()" << std::endl; |
| 154 | + #endif |
| 155 | + |
| 156 | + mActionAddMap->setEnabled(false); |
| 157 | + |
| 158 | + QModelIndexList indexes = mTree->selectionModel()->selectedIndexes(); |
| 159 | + |
| 160 | + mTextBrowser->clear(); |
| 161 | + |
| 162 | + QList<QModelIndex>::const_iterator it = indexes.begin(); |
| 163 | + for (; it != indexes.end(); ++it) |
| 164 | + { |
| 165 | + mTextBrowser->append ( mModel->itemInfo(*it) ); |
| 166 | + |
| 167 | + int type = mModel->itemType(*it); |
| 168 | + if ( type == QgsGrassModel::Raster || type == QgsGrassModel::VectorLayer ) |
| 169 | + { |
| 170 | + mActionAddMap->setEnabled(true); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | +} |
| 175 | + |
| 176 | +void QgsGrassBrowser::currentChanged(const QModelIndex & current, const QModelIndex & previous) |
| 177 | +{ |
| 178 | + #ifdef QGISDEBUG |
| 179 | + std::cerr << "QgsGrassBrowser::currentChanged()" << std::endl; |
| 180 | + #endif |
| 181 | +} |
| 182 | + |
0 commit comments