SilentEye 0.4.1
|
00001 // This file is part of SilentEye. 00002 // 00003 // SilentEye is free software: you can redistribute it and/or modify 00004 // it under the terms of the GNU General Public License as published by 00005 // the Free Software Foundation, either version 3 of the License, or 00006 // (at your option) any later version. 00007 // 00008 // SilentEye is distributed in the hope that it will be useful, 00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 // GNU General Public License for more details. 00012 // 00013 // You should have received a copy of the GNU General Public License 00014 // along with SilentEye. If not, see <http://www.gnu.org/licenses/>. 00015 00016 #include "mainwindow.h" 00017 #include "controller.h" 00018 00019 #include <audio.h> 00020 00021 namespace SilentEye { 00022 00023 MainWindow::MainWindow(QString url, QWidget* parent) 00024 : QMainWindow(parent) 00025 { 00026 setupUi(this); 00027 m_logger = new Logger(this); 00028 00029 /* center the window */ 00030 QRect rect = QApplication::desktop()->availableGeometry(this); 00031 move(rect.center() - this->rect().center()); 00032 00033 m_hasMediaLoaded = false; 00034 tabWidget->clear(); 00035 tabWidget->addTab(new MediaWidget(tabWidget), QString("No media selected")); 00036 00037 connectSignals(); 00038 if (!url.isEmpty()) { 00039 newMedia(url); 00040 } 00041 } 00042 00043 MainWindow::~MainWindow() 00044 { 00045 if (!m_currentSound.isNull()) 00046 delete m_currentSound; 00047 delete(contextMenu); 00048 delete(m_logger); 00049 } 00050 00051 void MainWindow::addMediaTab(QPointer<Media> md) 00052 { 00053 closeTabByPath(md->filePath()); 00054 00055 MediaWidget* w; 00056 if(!m_hasMediaLoaded) 00057 { 00058 w = (MediaWidget*)tabWidget->currentWidget(); 00059 if (md->type() == Media::IMAGE) { 00060 w->setPixmap(*((Image*)md.data())); 00061 } else if (md->type() == Media::AUDIO) { 00062 w->setPixmap(QPixmap(QString::fromUtf8(":/icons/img/audio.png")), false); 00063 w->showPlayer(true); 00064 connect(w, SIGNAL(play()), this, SLOT(playMedia())); 00065 connect(w, SIGNAL(stop()), this, SLOT(stopMedia())); 00066 } else { 00067 w->setText(md->shortName()); 00068 } 00069 tabWidget->setTabText(tabWidget->currentIndex(), md->shortName()); 00070 } 00071 else 00072 { 00073 w = new MediaWidget(tabWidget); 00074 if (md->type() == Media::IMAGE) { 00075 w->setPixmap(*((Image*)md.data())); 00076 } else if (md->type() == Media::AUDIO) { 00077 w->setPixmap(QPixmap(QString::fromUtf8(":/icons/img/audio.png")), false); 00078 w->showPlayer(true); 00079 connect(w, SIGNAL(play()), this, SLOT(playMedia())); 00080 connect(w, SIGNAL(stop()), this, SLOT(stopMedia())); 00081 } else { 00082 w->setText(md->shortName()); 00083 } 00084 tabWidget->addTab(w, md->shortName()); 00085 } 00086 w->setContextMenu(contextMenu); 00087 w->setFilepath(md->filePath()); 00088 closeTabButton->setVisible(true); 00089 m_mediaMap[md->filePath()]=md; 00090 setEnabledImageActions(true); 00091 m_hasMediaLoaded = true; 00092 tabWidget->setTabToolTip(tabWidget->count()-1, md->filePath()); 00093 tabWidget->setCurrentIndex(tabWidget->count()-1); 00094 } 00095 00096 void MainWindow::closeCurrentTab() 00097 { 00098 00099 if (!m_currentSound.isNull()) 00100 { 00101 m_currentSound->stop(); 00102 delete m_currentSound; 00103 } 00104 if(tabWidget->count()==1) 00105 { 00106 m_hasMediaLoaded = false; 00107 setEnabledImageActions(false); 00108 closeTabButton->setVisible(false); 00109 } 00110 tabWidget->removeTab(tabWidget->currentIndex()); 00111 00112 if(!m_hasMediaLoaded) 00113 tabWidget->addTab(new MediaWidget(tabWidget), "No media selected"); 00114 } 00115 00116 void MainWindow::closeTabByPath(QString path) 00117 { 00118 if( !m_mediaMap.contains(path) ) 00119 return; 00120 00121 int i = 0; 00122 while(i<tabWidget->count()) 00123 { 00124 QString currentPath = ((MediaWidget*) tabWidget->widget(i))->filepath(); 00125 if(currentPath == path) 00126 { 00127 if (!m_currentSound.isNull()) 00128 { 00129 m_currentSound->stop(); 00130 delete m_currentSound; 00131 } 00132 tabWidget->removeTab(i); 00133 delete(m_mediaMap[path]); 00134 m_mediaMap.remove(path); 00135 return; 00136 } 00137 i++; 00138 } 00139 } 00140 00141 void MainWindow::setEnabledImageActions(const bool value) 00142 { 00143 decodeButton->setEnabled(value); 00144 actionDecode->setEnabled(value); 00145 encodeButton->setEnabled(value); 00146 actionEncode->setEnabled(value); 00147 propertyButton->setEnabled(value); 00148 actionProperty->setEnabled(value); 00149 actionProperty->setEnabled(value); 00150 actionCloseCurrent->setEnabled(value); 00151 actionCopy->setEnabled(value); 00152 } 00153 00154 void MainWindow::dragEnterEvent(QDragEnterEvent *event) 00155 { 00156 if (event->mimeData()->hasUrls()) 00157 event->acceptProposedAction(); 00158 } 00159 00160 void MainWindow::dropEvent(QDropEvent *event) 00161 { 00162 if(event->mimeData()->hasUrls()) 00163 { 00164 QList<QUrl> urls = event->mimeData()->urls(); 00165 for(int i=0; i<urls.size(); i++) 00166 { 00167 if(urls.at(i).toLocalFile().length()>0) 00168 newMedia(urls.at(i).toLocalFile()); 00169 } 00170 } 00171 else 00172 { 00173 QMessageBox::warning(this, tr("SilentEye Warning"), 00174 "The dropped object doesn't have any url."); 00175 } 00176 00177 } 00178 00179 QPointer<Media> MainWindow::currentMedia() 00180 { 00181 QString key = ((MediaWidget*) tabWidget->widget(tabWidget->currentIndex()))->filepath(); 00182 if(m_mediaMap.contains(key)) 00183 return m_mediaMap[key]; 00184 else 00185 return NULL; 00186 } 00187 00188 void MainWindow::newMedia(QString url) 00189 { 00190 if(QFile::exists(url)) 00191 { 00192 if (url.endsWith(".wav", Qt::CaseInsensitive)) { 00193 QPointer<Media> md; 00194 try { 00195 md = new Audio(url); 00196 } catch (SilentEyeException e) { 00197 QMessageBox msgBox; 00198 msgBox.setIcon(QMessageBox::Critical); 00199 msgBox.setWindowTitle("SilentEye error"); 00200 msgBox.setText(e.message()); 00201 msgBox.setDetailedText(e.details()); 00202 msgBox.exec(); 00203 m_logger->warning("Cannot load '" + url + "'\n" + e.details()); 00204 return; 00205 } 00206 addMediaTab(md); 00207 } else if(url.endsWith(".mp3", Qt::CaseInsensitive) || url.endsWith(".ogg", Qt::CaseInsensitive) 00208 || url.endsWith(".wma", Qt::CaseInsensitive) || url.endsWith(".flac", Qt::CaseInsensitive)) { 00209 QMessageBox::warning(this, tr("SilentEye Warning"), 00210 "Cannot load '" + url + "'\nUnsupported audio format, you must load WAVE file."); 00211 return; 00212 } else { 00213 QPointer<Image> img = new Image(url); 00214 if(img->isNull()) 00215 { 00216 QMessageBox::warning(this, tr("SilentEye Warning"), 00217 "Cannot load '" + url + "'\nFile is not a valid media."); 00218 return; 00219 } 00220 addMediaTab(QPointer<Media>(img)); 00221 } 00222 } 00223 else 00224 { 00225 m_logger->warning("Cannot load '" + url + "'\n file doesn't exist !"); 00226 } 00227 } 00228 00229 void MainWindow::openFile() 00230 { 00231 setCursor(Qt::WaitCursor); 00232 QFileDialog dialog(this, tr("Open Media")); 00233 dialog.setFilter(tr("Media Files (*.png *.jpg .jpeg *.bmp *.tiff *.tif *.wav)")); 00234 dialog.setViewMode(QFileDialog::List); 00235 dialog.setFileMode(QFileDialog::ExistingFiles); 00236 dialog.setDirectory(Controller::instance()->config.get("output")); 00237 if(dialog.exec()) 00238 { 00239 QStringList fileNames = dialog.selectedFiles(); 00240 for(int i=0; i<fileNames.size(); i++) 00241 { 00242 newMedia(fileNames.at(i)); 00243 } 00244 } 00245 setCursor(Qt::ArrowCursor); 00246 } 00247 00248 void MainWindow::paste() 00249 { 00250 const QClipboard *clipboard = QApplication::clipboard(); 00251 const QMimeData *mimeData = clipboard->mimeData(); 00252 00253 if (mimeData->hasImage()) { 00254 QPointer<Image> img = new Image(qvariant_cast<QPixmap>(mimeData->imageData())); 00255 img->setShortName("pasted_image.img"); 00256 addMediaTab(QPointer<Media>(img)); 00257 } else { 00258 QMessageBox::warning(this, tr("SilentEye Warning"), 00259 "Cannot paste clipboard's data: not a supported media!"); 00260 } 00261 } 00262 00263 void MainWindow::copy() 00264 { 00265 if (m_hasMediaLoaded) 00266 { 00267 QClipboard *clipboard = QApplication::clipboard(); 00268 clipboard->clear(); 00269 QPointer<Media> md = currentMedia(); 00270 if (md->type() == Media::IMAGE) { 00271 Image* img = (Image*) md.data(); 00272 clipboard->setPixmap(*img, QClipboard::Clipboard); 00273 } else { 00274 clipboard->setText(md->filePath()); 00275 } 00276 } 00277 } 00278 00279 void MainWindow::playMedia() 00280 { 00281 if(m_hasMediaLoaded) 00282 { 00283 Media* media = currentMedia(); 00284 if (media->type() == Media::AUDIO) { 00285 if (!m_currentSound.isNull()) 00286 delete m_currentSound; 00287 if (!QSound::isAvailable()) 00288 { 00289 QMessageBox::warning(this, tr("SilentEye Warning"), 00290 "Audio device is not ready!"); 00291 } 00292 else 00293 { 00294 m_currentSound = new QSound(media->filePath()); 00295 m_currentSound->play(); 00296 } 00297 } 00298 } 00299 } 00300 00301 void MainWindow::stopMedia() 00302 { 00303 if(m_hasMediaLoaded) 00304 { 00305 Media* media = currentMedia(); 00306 if (media->type() == Media::AUDIO && !m_currentSound.isNull()) { 00307 m_currentSound->stop(); 00308 } 00309 } 00310 } 00311 00312 void MainWindow::execPropertyDialog() 00313 { 00314 propertyDialog.setMedia(currentMedia()); 00315 propertyDialog.exec(); 00316 } 00317 00318 void MainWindow::execEncodeDialog() 00319 { 00320 if (!m_currentSound.isNull()) 00321 { 00322 m_currentSound->stop(); 00323 delete m_currentSound; 00324 } 00325 encodeDialog.setMedia(currentMedia()); 00326 encodeDialog.exec(); 00327 } 00328 00329 void MainWindow::execDecodeDialog() 00330 { 00331 if (!m_currentSound.isNull()) 00332 { 00333 m_currentSound->stop(); 00334 delete m_currentSound; 00335 } 00336 decodeDialog.setMedia(currentMedia()); 00337 decodeDialog.exec(); 00338 } 00339 00340 void MainWindow::execUpdateDialog() 00341 { 00342 updateDialog.exec(); 00343 } 00344 00345 void MainWindow::connectSignals() 00346 { 00347 // Button actions 00348 connect(encodeButton, SIGNAL(pressed()), this, SLOT(execEncodeDialog())); 00349 connect(actionEncode, SIGNAL(triggered()), this, SLOT(execEncodeDialog())); 00350 connect(propertyButton, SIGNAL(pressed()), this, SLOT(execPropertyDialog())); 00351 connect(actionProperty, SIGNAL(triggered()), this, SLOT(execPropertyDialog())); 00352 connect(decodeButton, SIGNAL(pressed()), this, SLOT(execDecodeDialog())); 00353 connect(actionDecode, SIGNAL(triggered()), this, SLOT(execDecodeDialog())); 00354 connect(actionCheckUpdate, SIGNAL(triggered()), this, SLOT(execUpdateDialog())); 00355 00356 // Menu actions 00357 connect(actionOpen, SIGNAL(triggered()), this, SLOT(openFile())); 00358 connect(actionCopy, SIGNAL(triggered()), this, SLOT(copy())); 00359 connect(actionPaste, SIGNAL(triggered()), this, SLOT(paste())); 00360 connect(actionCloseCurrent, SIGNAL(triggered()), this, SLOT(closeCurrentTab())); 00361 connect(actionPreferences, SIGNAL(triggered()), &preferenceDialog, SLOT(exec())); 00362 connect(actionAbout, SIGNAL(triggered()), &aboutDialog, SLOT(exec())); 00363 00364 // Others 00365 connect(&encodeDialog, SIGNAL(encodedImage(QString)), this, SLOT(newMedia(QString))); 00366 00367 contextMenu = new QMenu(this); 00368 contextMenu->addAction(actionCloseCurrent); 00369 contextMenu->addSeparator(); 00370 contextMenu->addAction(actionEncode); 00371 contextMenu->addAction(actionDecode); 00372 contextMenu->addAction(actionProperty); 00373 contextMenu->addSeparator(); 00374 contextMenu->addAction(actionCopy); 00375 00376 // close tab button 00377 closeTabButton = new QPushButton(this); 00378 closeTabButton->setIcon(QIcon(":/menu/img/close.png")); 00379 closeTabButton->setFlat(true); 00380 tabWidget->setCornerWidget(closeTabButton); 00381 closeTabButton->setVisible(false); 00382 connect(closeTabButton, SIGNAL(pressed()), this, SLOT(closeCurrentTab())); 00383 } 00384 00385 }