00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "mainwin.h"
00021 #include <QGraphicsScene>
00022 #include <QFileDialog>
00023 #include <QMessageBox>
00024 #include <QGraphicsPixmapItem>
00025 #include <QList>
00026 #include <QCompleter>
00027 #include <QDir>
00028 #include <QSettings>
00029 #include <QWhatsThis>
00030 #include <iostream>
00031
00035 MainWin::MainWin(QWidget *p)
00036 : QMainWindow(p) {
00037 m_ui.setupUi(this);
00038
00039 m_tagsuche = NULL;
00040
00041 m_bild = new QGraphicsScene();
00042 m_ui.bild->setScene(m_bild);
00043
00044 m_bildauswahl = new Bildauswahl(this);
00045 addToolBar(Qt::TopToolBarArea, m_bildauswahl);
00046
00047 m_ui.menuHilfe->insertAction(m_ui.actionAbout, QWhatsThis::createAction(m_ui.menuHilfe));
00048 m_ui.menuHilfe->insertSeparator(m_ui.actionAbout);
00049 QSettings settings;
00050
00051 QObject::connect(m_ui.actionNeu, SIGNAL(triggered()),
00052 this, SLOT(newPicture()));
00053 QObject::connect(m_ui.actionNeue_Bilder, SIGNAL(triggered()),
00054 this, SLOT(findNewPictures()));
00055 QObject::connect(m_ui.actionSpeichern, SIGNAL(triggered()),
00056 this, SLOT(saveData()));
00057 QObject::connect(m_ui.actionSuchen, SIGNAL(triggered()),
00058 this, SLOT(search()));
00059 QObject::connect(m_ui.actionAbout, SIGNAL(triggered()),
00060 this, SLOT(about()));
00061 QObject::connect(m_ui.actionAboutQT, SIGNAL(triggered()),
00062 qApp, SLOT(aboutQt()));
00063 QObject::connect(m_ui.actionOptionen, SIGNAL(triggered()),
00064 this, SLOT(options()));
00065 QObject::connect(m_ui.ZoomIn, SIGNAL(clicked()),
00066 this, SLOT(zoomIn()));
00067 QObject::connect(m_ui.ZoomOut, SIGNAL(clicked()),
00068 this, SLOT(zoomOut()));
00069 QObject::connect(m_ui.ZoomOriginal, SIGNAL(clicked()),
00070 this, SLOT(zoomOriginal()));
00071 QObject::connect(m_ui.FitWidth, SIGNAL(clicked()),
00072 this, SLOT(fitWidth()));
00073 QObject::connect(m_ui.FitHeight, SIGNAL(clicked()),
00074 this, SLOT(fitHeight()));
00075 QObject::connect(m_ui.tagInput, SIGNAL(returnPressed()),
00076 this, SLOT(newTag()));
00077 QObject::connect(m_ui.tagSelect, SIGNAL(itemSelectionChanged ()),
00078 this, SLOT(setDirty()));
00079 QObject::connect(m_bildauswahl, SIGNAL(bildGeaendert(const QString&)),
00080 this, SLOT(displayPicture(const QString&)));
00081
00082
00083 QString dbPath = settings.value("db/path", "*FALSE*").toString();
00084 if("*FALSE*" == dbPath) {
00085 QDir tmpPath(QDir::homePath()+"/.imageTagger");
00086 if(!tmpPath.exists()) {
00087 tmpPath.mkpath(tmpPath.absolutePath());
00088 }
00089 dbPath = tmpPath.absolutePath();
00090 settings.setValue("db/path", dbPath);
00091 }
00092 m_datenbank = new Datenbank(dbPath);
00093 m_ui.tagSelect->addItems(m_datenbank->getAllTags());
00094 m_ui.tagInput->setCompleter(new QCompleter(m_ui.tagSelect->model()));
00095
00096 bool ok;
00097 DisplayMode dpm = static_cast<DisplayMode>(settings.value("defaultDisplayMode", "*FALSE*").toInt(&ok));
00098 if(ok) {
00099 m_defaultDisplayMode = dpm;
00100 } else {
00101 m_defaultDisplayMode = DM_ORIGINAL;
00102 }
00103
00104 m_path = QDir::currentPath();
00105 m_dirty = false;
00106 }
00107
00112 void MainWin::newPicture() {
00113 newPictureList(QFileDialog::getOpenFileNames(this, tr("Bild oeffnen"), m_path,
00114 tr("Images (*.png *.jpg *.jpeg *.JPG *.xpm *.gif)")));
00115 }
00116
00125 void MainWin::displayPicture(const QString &bildName) {
00126 if(m_dirty) {
00127 if(QMessageBox::Yes == QMessageBox::question(this,
00128 tr("Tags wurden geaendert"),
00129 tr("Speichern?"),
00130 QMessageBox::Yes|QMessageBox::No,
00131 QMessageBox::Yes)) {
00132 saveData();
00133 }
00134 }
00135
00136 QList<QGraphicsItem *> bilder = m_bild->items();
00137 for(QList<QGraphicsItem *>::iterator bild = bilder.begin();
00138 bilder.end() != bild;
00139 bild++) {
00140 m_bild->removeItem(*bild);
00141 }
00142
00143 m_bildname = bildName;
00144
00145 if(!m_bildname.isEmpty()) {
00146 QImage showBild(m_bildname);
00147 if(showBild.isNull()) {
00148 QMessageBox::warning(this, tr("Fehler"),
00149 tr("Bild %1 konnte nicht geladen werden").arg(m_bildname) );
00150 } else {
00151 QRectF bildSize;
00152 m_bild->addItem(new QGraphicsPixmapItem(QPixmap::fromImage(showBild)));
00153 bildSize = m_bild->itemsBoundingRect();
00154 m_ui.bild->setSceneRect(bildSize);
00155 switch(m_defaultDisplayMode) {
00156 case DM_ORIGINAL:
00157 zoomOriginal();
00158 break;
00159 case DM_WIDTH:
00160 fitWidth();
00161 break;
00162 case DM_MAXWIDTH:
00163 if(m_ui.bild->width() < bildSize.width()) {
00164 fitWidth();
00165 } else {
00166 zoomOriginal();
00167 }
00168 break;
00169 case DM_HEIGHT:
00170 fitHeight();
00171 break;
00172 case DM_MAXHEIGHT:
00173 if(m_ui.bild->height() < bildSize.height()) {
00174 fitHeight();
00175 } else {
00176 zoomOriginal();
00177 }
00178 break;
00179 }
00180 }
00181 } else {
00182 m_ui.bild->setSceneRect(m_bild->itemsBoundingRect());
00183 }
00184
00185 statusBar()->showMessage(m_bildname);
00186
00187 m_path = m_bildname.left(m_bildname.lastIndexOf('/'));
00188
00189
00190 QList<QListWidgetItem*> selectedTags = m_ui.tagSelect->selectedItems();
00191 QList<QListWidgetItem*>::iterator itTag;
00192 for(itTag = selectedTags.begin();
00193 selectedTags.end() != itTag;
00194 itTag++) {
00195 (*itTag)->setSelected(false);
00196 }
00197 QStringList taglist = m_datenbank->getTags(m_bildname);
00198 QStringList::iterator itNewtag;
00199 for(itNewtag=taglist.begin();
00200 taglist.end() != itNewtag;
00201 itNewtag++) {
00202 m_ui.tagSelect->findItems(*itNewtag, Qt::MatchExactly).first()->setSelected(true);
00203 }
00204 m_dirty = false;
00205 }
00206
00210 void MainWin::findNewPictures() {
00211 QStringList newImages;
00212
00213 QString pathName = QFileDialog::getExistingDirectory(this, tr("Bilderpfad"), m_path);
00214 if("" != pathName) {
00215 QDir path(pathName);
00216 QStringList bildtyp;
00217 bildtyp << "*.png" << "*.jpg" << "*.jpeg" << "*.xpm" << "*.gif";
00218 QStringList dirBilder = path.entryList(bildtyp, QDir::Files|QDir::Readable, QDir::Name);
00219 if(!dirBilder.empty()) {
00220 QStringList alleBilder = m_datenbank->getAllImages();
00221 alleBilder.sort();
00222
00223 int i=0, j=0;
00224 while(i<dirBilder.size() && j < alleBilder.size()) {
00225 if(!dirBilder[i].startsWith(pathName))
00226 dirBilder[i] = pathName + dirBilder[i];
00227 if(dirBilder[i] < alleBilder[j]) {
00228 newImages << dirBilder[i++];
00229 } else if(dirBilder[i] > alleBilder[j]) {
00230 j++;
00231 } else {
00232 i++;
00233 j++;
00234 }
00235 }
00236 while(i<dirBilder.size()) {
00237 if(!dirBilder[i].startsWith(pathName))
00238 dirBilder[i] = pathName + dirBilder[i];
00239 newImages << dirBilder[i++];
00240 }
00241 }
00242 }
00243
00244 if(newImages.empty()) {
00245 QMessageBox::information(this, tr("Suche"), tr("keine neuen Bilder gefunden"));
00246 } else {
00247 newPictureList(newImages);
00248 }
00249
00250 }
00251
00258 void MainWin::saveData() {
00259 QStringList taglist;
00260 QList<QListWidgetItem*> selectedTags = m_ui.tagSelect->selectedItems();
00261 QList<QListWidgetItem*>::iterator itTag;
00262 for(itTag = selectedTags.begin();
00263 selectedTags.end() != itTag;
00264 itTag++) {
00265 taglist.append((*itTag)->text());
00266 }
00267 m_datenbank->writeTags(m_bildname, taglist);
00268 m_dirty = false;
00269 }
00270
00274 void MainWin::about() {
00275 QMessageBox::about(this, tr("Ueber imageTagger"),
00276 tr("imageTagger ist ein einfaches Programm zum organisieren von "
00277 "Bildersammlungen ueber Tags."
00278 "<p>"
00279 "©2008 <a href=\"mailto:bretscher@5sl.org\">Johannes Bretscher</a>"
00280 "<p>"
00281 "This program is distributed in the hope that it will be useful, "
00282 "but WITHOUT ANY WARRANTY; without even the implied warranty of "
00283 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the "
00284 "GNU General Public License for more details. "));
00285 }
00286
00293 void MainWin::zoomIn() {
00294 m_ui.bild->scale(2.0, 2.0);
00295 }
00296
00303 void MainWin::zoomOut() {
00304 m_ui.bild->scale(0.5, 0.5);
00305 }
00306
00310 void MainWin::zoomOriginal() {
00311 m_ui.bild->resetMatrix();
00312 }
00313
00317 void MainWin::fitWidth() {
00318 float zoom = m_ui.bild->width() / m_ui.bild->sceneRect().width();
00319
00320 m_ui.bild->setMatrix(QMatrix(zoom, 0.0, 0.0, zoom, 0.0, 0.0), false);
00321 }
00322
00326 void MainWin::fitHeight() {
00327 float zoom = m_ui.bild->height() / m_ui.bild->sceneRect().height();
00328
00329 m_ui.bild->setMatrix(QMatrix(zoom, 0.0, 0.0, zoom, 0.0, 0.0), false);
00330 }
00331
00338 void MainWin::newTag() {
00339 QString tagText = m_ui.tagInput->text();
00340 if("" == tagText)
00341 return;
00342
00343 QList<QListWidgetItem*> tagList = m_ui.tagSelect->findItems(tagText, Qt::MatchExactly);
00344 if(!tagList.empty()) {
00345 tagList.first()->setSelected(true);
00346 } else {
00347 QListWidgetItem *newTagItem = new QListWidgetItem(tagText, m_ui.tagSelect);
00348 newTagItem->setSelected(true);
00349 }
00350 m_ui.tagInput->clear();
00351
00352 setDirty();
00353 }
00354
00361 void MainWin::setDirty() {
00362 m_dirty = true;
00363 }
00364
00369 void MainWin::search() {
00370 m_tagsuche = new Tagsuche(m_datenbank, this);
00371 QObject::connect(m_tagsuche, SIGNAL(selected(const QStringList &)),
00372 this, SLOT(newPictureList(const QStringList &)));
00373 m_tagsuche->show();
00374 }
00375
00376 void MainWin::options() {
00377 Optionen optionen(this);
00378 QObject::connect(&optionen, SIGNAL(setDefaultZoom(DisplayMode)),
00379 this, SLOT(changeDefaultZoom(DisplayMode)));
00380 optionen.exec();
00381
00382 }
00387 void MainWin::newPictureList(const QStringList &list) {
00388
00389 m_bildauswahl->setImageList(list);
00390 }
00391
00397 void MainWin::changeDefaultZoom(DisplayMode newDM) {
00398
00399 if(newDM != m_defaultDisplayMode) {
00400 QSettings settings;
00401 settings.setValue("defaultDisplayMode", newDM);
00402 m_defaultDisplayMode = newDM;
00403 }
00404 }