| 1 | /***************************************************************************** |
| 2 | ** QNapi |
| 3 | ** Copyright (C) 2008-2017 Piotr KrzemiĆski <[email protected]> |
| 4 | ** |
| 5 | ** This program is free software; you can redistribute it and/or modify |
| 6 | ** it under the terms of the GNU General Public License as published by |
| 7 | ** the Free Software Foundation; either version 2 of the License, or |
| 8 | ** (at your option) any later version. |
| 9 | ** |
| 10 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
| 11 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | ** |
| 13 | *****************************************************************************/ |
| 14 | |
| 15 | #include "libqnapi.h" |
| 16 | |
| 17 | #include "frmlistsubtitles.h" |
| 18 | #include "subdatawidget.h" |
| 19 | |
| 20 | #include <QDesktopWidget> |
| 21 | #include <QMessageBox> |
| 22 | #include <QtWidgets/QHeaderView> |
| 23 | |
| 24 | frmListSubtitles::frmListSubtitles(QWidget *parent, Qt::WindowFlags f) |
| 25 | : QDialog(parent, f), |
| 26 | enginesRegistry(LibQNapi::subtitleDownloadEngineRegistry()) { |
| 27 | ui.setupUi(this); |
| 28 | |
| 29 | QRect position = frameGeometry(); |
| 30 | position.moveCenter(QDesktopWidget().availableGeometry().center()); |
| 31 | move(position.topLeft()); |
| 32 | } |
| 33 | |
| 34 | void frmListSubtitles::setFileName(const QString &name) { |
| 35 | ui.lbSelectSubtitles->setText(tr("Select the subtitles which you want to " |
| 36 | "download for video file<br><br><b>%1</b>") |
| 37 | .arg(name)); |
| 38 | } |
| 39 | |
| 40 | void frmListSubtitles::setSubtitlesList(QList<SubtitleInfo> list) { |
| 41 | ui.twSubtitles->clear(); |
| 42 | |
| 43 | int i = 0, goodCount = 0, badCount = 0; |
| 44 | foreach (SubtitleInfo s, list) { |
| 45 | bool highlight = (s.resolution != SUBTITLE_UNKNOWN); |
| 46 | |
| 47 | QBrush brush((s.resolution == SUBTITLE_GOOD) ? QColor(qRgb(200, 255, 200)) |
| 48 | : QColor(qRgb(255, 200, 200))); |
| 49 | |
| 50 | QListWidgetItem *listItem = new QListWidgetItem(); |
| 51 | |
| 52 | ui.twSubtitles->addItem(listItem); |
| 53 | |
| 54 | subDataWidget *subData = new subDataWidget(); |
| 55 | |
| 56 | if (highlight) listItem->setBackground(brush); |
| 57 | |
| 58 | QString lang_path = QString(":/languages/") + s.lang + ".png"; |
| 59 | |
| 60 | if (highlight && (s.resolution == SUBTITLE_GOOD)) { |
| 61 | ++goodCount; |
| 62 | } else if (highlight && (s.resolution == SUBTITLE_BAD)) { |
| 63 | ++badCount; |
| 64 | } |
| 65 | |
| 66 | subData->setSubData( |
| 67 | s.name, s.format, QIcon(lang_path), |
| 68 | QIcon(QPixmap(enginesRegistry->enginePixmapData(s.engine)))); |
| 69 | |
| 70 | ui.twSubtitles->setItemWidget(listItem, subData); |
| 71 | listItem->setSizeHint(subData->sizeHint()); |
| 72 | |
| 73 | ++i; |
| 74 | } |
| 75 | |
| 76 | if (goodCount == 0) ui.lbGreenHint->hide(); |
| 77 | if (badCount == 0) ui.lbRedHint->hide(); |
| 78 | } |
| 79 | |
| 80 | int frmListSubtitles::getSelectedIndex() { |
| 81 | QModelIndexList selected = |
| 82 | ui.twSubtitles->selectionModel()->selectedIndexes(); |
| 83 | return selected.first().row(); |
| 84 | } |
| 85 | |
| 86 | void frmListSubtitles::accept() { |
| 87 | if (ui.twSubtitles->selectedItems().size() == 0) { |
| 88 | QMessageBox::warning(this, tr("No subtitle selected"), |
| 89 | tr("You need to select a subtitle from the list!")); |
| 90 | } else { |
| 91 | QDialog::accept(); |
| 92 | } |
| 93 | } |