File indexing completed on 2024-05-12 16:23:33

0001 /***************************************************************************
0002  *   Copyright (C) 2005-2017 by Linuxstopmotion contributors;              *
0003  *   see the AUTHORS file for details.                                     *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 #include "importtab.h"
0021 
0022 #include "flexiblelineedit.h"
0023 #include "preferencestool.h"
0024 #include "graphics/icons/close.xpm"
0025 
0026 #include <algorithm>
0027 #include <memory>
0028 #include <QLayout>
0029 #include <QLabel>
0030 #include <QGridLayout>
0031 #include <QHeaderView>
0032 
0033 class QResizeEvent;
0034 
0035 ImportTab::ImportTab( QWidget *parent ) : QWidget(parent)
0036 {
0037     deviceSelectionTable = 0;
0038     addButton            = 0;
0039     removeButton         = 0;
0040     changeButton         = 0;
0041     closeChangeBoxButton = 0;
0042     prePollEdit          = 0;
0043     startDaemonEdit      = 0;
0044     stopDaemonEdit       = 0;
0045     grabberPreferences   = 0;
0046     prePollLabel         = 0;
0047     startDaemonLabel     = 0;
0048     stopDaemonLabel      = 0;
0049     checkTableItem       = 0;
0050     informationText      = 0;
0051 
0052     makeGUI();
0053 }
0054 
0055 
0056 void ImportTab::makeGUI()
0057 {
0058     this->setFocusPolicy(Qt::ClickFocus);
0059 
0060     informationText = new QTextEdit;
0061     informationText->setReadOnly(true);
0062     informationText->setHtml(
0063         "<p>" + tr("Below you can set which program/process Stopmotion should use "
0064         "for grabbing images from the selected device.") + "</p><p>" +
0065         tr("You should always use <b>$VIDEODEVICE</b> and <b>$IMAGEFILE</b> to represent "
0066         "the video device and the image file, respectively.") + "</p>");
0067     informationText->setMinimumWidth(440);
0068     informationText->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
0069 
0070     QStringList lst;
0071     lst << tr("Name") << tr("Description");
0072 
0073     deviceSelectionTable = new QTableWidget;
0074     deviceSelectionTable->setColumnCount(2);
0075     deviceSelectionTable->setRowCount(0);
0076     deviceSelectionTable->setSelectionMode(QAbstractItemView::SingleSelection);
0077     deviceSelectionTable->setSelectionBehavior(QAbstractItemView::SelectRows);
0078     deviceSelectionTable->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
0079     deviceSelectionTable->setHorizontalHeaderLabels(lst);
0080     deviceSelectionTable->verticalHeader()->setVisible(false);
0081 
0082     connect(deviceSelectionTable, SIGNAL(cellClicked(int, int)),
0083             this, SLOT(activeCellChanged(int, int)));
0084     connect(deviceSelectionTable, SIGNAL(cellChanged(int, int)),
0085             this, SLOT(contentsChanged(int, int)));
0086 
0087     addButton = new QPushButton(tr("&Add"));
0088     addButton->setFocusPolicy( Qt::NoFocus );
0089     QObject::connect(addButton, SIGNAL(clicked()), this, SLOT(addImportProgram()));
0090 
0091     removeButton = new QPushButton(tr("&Remove"));
0092     QObject::connect( removeButton, SIGNAL(clicked()), this, SLOT(removeImportProgram()));
0093 
0094     changeButton = new QPushButton(tr("&Edit"));
0095     QObject::connect( changeButton, SIGNAL(clicked()), this, SLOT(changeSettings()));
0096 
0097     grabberPreferences = new QGroupBox;
0098     grabberPreferences->setTitle(tr("Import device settings"));
0099     grabberPreferences->hide();
0100 
0101     closeChangeBoxButton = new QPushButton;
0102     closeChangeBoxButton->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
0103     closeChangeBoxButton->setIcon(QPixmap(closeicon));
0104     closeChangeBoxButton->setFlat(true);
0105     QObject::connect( closeChangeBoxButton, SIGNAL(clicked()),this, SLOT(closeChangeBox()));
0106 
0107     prePollLabel = new QLabel( tr("Pre-poll command") );
0108     prePollEdit = new FlexibleLineEdit;
0109     prePollLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
0110     prePollEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
0111     QObject::connect( prePollEdit, SIGNAL(textChanged(const QString &)),
0112             this, SLOT(updatePrePollString(const QString &)));
0113 
0114     startDaemonLabel = new QLabel( tr("Start daemon") );
0115     startDaemonEdit = new FlexibleLineEdit;
0116     startDaemonLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
0117     startDaemonEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
0118     QObject::connect( startDaemonEdit, SIGNAL(textChanged(const QString &)),
0119             this, SLOT(updateStartDaemonString(const QString &)));
0120 
0121     stopDaemonLabel = new QLabel( tr("Stop daemon") );
0122     stopDaemonEdit = new FlexibleLineEdit;
0123     stopDaemonLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
0124     stopDaemonEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
0125     QObject::connect( stopDaemonEdit, SIGNAL(textChanged(const QString &)),
0126             this, SLOT(updatestopDaemonString(const QString &)) );
0127 
0128     QVBoxLayout *mainLayout = new QVBoxLayout;
0129     mainLayout->addWidget(informationText);
0130     QVBoxLayout *buttonLayout = new QVBoxLayout;
0131     buttonLayout->setContentsMargins(0, 0, 0, 0);
0132     buttonLayout->setSpacing(2);
0133     buttonLayout->addStretch(1);
0134     buttonLayout->addWidget(addButton);
0135     buttonLayout->addWidget(removeButton);
0136     buttonLayout->addWidget(changeButton);
0137     QHBoxLayout *deviceLayout = new QHBoxLayout;
0138     deviceLayout->addWidget(deviceSelectionTable);
0139     deviceLayout->addLayout(buttonLayout);
0140     mainLayout->addLayout(deviceLayout);
0141     mainLayout->addWidget(grabberPreferences);
0142     setLayout(mainLayout);
0143 
0144     QGridLayout *grabberPrefsLayout = new QGridLayout;
0145     grabberPrefsLayout->addWidget(closeChangeBoxButton, 0, 2, Qt::AlignLeading);
0146     grabberPrefsLayout->addWidget(prePollLabel, 0, 0, Qt::AlignTrailing);
0147     grabberPrefsLayout->addWidget(prePollEdit, 0, 1);
0148     grabberPrefsLayout->addWidget(startDaemonLabel, 1, 0, Qt::AlignLeading);
0149     grabberPrefsLayout->addWidget(startDaemonEdit, 1, 1, 1, 2);
0150     grabberPrefsLayout->addWidget(stopDaemonLabel, 2, 0, Qt::AlignLeading);
0151     grabberPrefsLayout->addWidget(stopDaemonEdit, 2, 1, 1, 2);
0152     grabberPrefsLayout->setColumnStretch(1, 1);
0153     grabberPreferences->setLayout(grabberPrefsLayout);
0154 }
0155 
0156 
0157 void ImportTab::initializeImportValues()
0158 {
0159     PreferencesTool* pref = PreferencesTool::get();
0160 
0161     int numImports = pref->getPreference("numberofimports", 0);
0162     deviceSelectionTable->setRowCount(numImports);
0163     for (int i = 0; i < numImports; ++i) {
0164         Preference name(QString("importname%1").arg(i).toLatin1().constData(), "");
0165         deviceSelectionTable->setItem( i, 0, new QTableWidgetItem(name.get()) );
0166         Preference desc(QString("importdescription%1").arg(i).toLatin1().constData(), "");
0167         deviceSelectionTable->setItem( i, 1, new QTableWidgetItem(desc.get()) );
0168 
0169         Preference prepoll(QString("importprepoll%1").arg(i).toLatin1().constData(), "");
0170         prePollStrings.push_back(QString(prepoll.get()));
0171 
0172         Preference start(QString("importstartdaemon%1").arg(i).toLatin1().constData(), "");
0173         startDaemonStrings.push_back(QString(start.get()));
0174 
0175         Preference stop(QString("importstopdaemon%1").arg(i).toLatin1().constData(), "");
0176         stopDaemonStrings.push_back(QString(stop.get()));
0177     }
0178 
0179     int activeCommand = pref->getPreference("activedevice", -1);
0180     if (activeCommand > -1) {
0181         deviceSelectionTable->setCurrentCell(activeCommand, 0);
0182     }
0183 }
0184 
0185 
0186 void ImportTab::apply() {
0187     PreferencesTool *prefs = PreferencesTool::get();
0188 
0189     // Remove old preferences
0190     int numImports = prefs->getPreference("numberofimports", -1);
0191     if (numImports > 0) {
0192         for (int i = 0; i < numImports; ++i) {
0193             prefs->removePreference(QString("importname%1").arg(i).toLatin1().constData());
0194             prefs->removePreference(QString("importdescription%1").arg(i).toLatin1().constData());
0195             prefs->removePreference(QString("importprepoll%1").arg(i).toLatin1().constData());
0196             prefs->removePreference(QString("importstartdaemon%1").arg(i).toLatin1().constData());
0197             prefs->removePreference(QString("importstopdaemon%1").arg(i).toLatin1().constData());
0198         }
0199     }
0200 
0201     numImports = deviceSelectionTable->rowCount();
0202     if (numImports > 0) {
0203         prefs->setPreference("numberofimports", numImports);
0204         prefs->setPreference("activedevice", deviceSelectionTable->currentRow());
0205         for (int i = 0; i < numImports; ++i) {
0206             prefs->setPreference(QString("importname%1").arg(i).toLatin1().constData(),
0207                     deviceSelectionTable->item(i, 0)->text().toLatin1().constData());
0208             prefs->setPreference(QString("importdescription%1").arg(i).toLatin1().constData(),
0209                     deviceSelectionTable->item(i, 1)->text().toLatin1().constData());
0210             prefs->setPreference(QString("importprepoll%1").arg(i).toLatin1().constData(),
0211                     prePollStrings[i].toLatin1().constData());
0212             prefs->setPreference(QString("importstartdaemon%1").arg(i).toLatin1().constData(),
0213                     startDaemonStrings[i].toLatin1().constData());
0214             prefs->setPreference(QString("importstopdaemon%1").arg(i).toLatin1().constData(),
0215                     stopDaemonStrings[i].toLatin1().constData());
0216         }
0217     } else {
0218         prefs->setPreference("numberofimports", -1);
0219         prefs->setPreference("activedevice", -1);
0220     }
0221 }
0222 
0223 
0224 void ImportTab::resizeEvent(QResizeEvent *event)
0225 {
0226     contentsChanged(0, 0);
0227     QWidget::resizeEvent(event);
0228 }
0229 
0230 
0231 void ImportTab::addImportProgram()
0232 {
0233     int newRow = deviceSelectionTable->rowCount();
0234     deviceSelectionTable->setRowCount(newRow + 1);
0235     deviceSelectionTable->setItem( newRow, 0, new QTableWidgetItem(QString("")) );
0236     deviceSelectionTable->setItem( newRow, 1, new QTableWidgetItem(QString("")) );
0237     deviceSelectionTable->setCurrentCell(newRow, 0);
0238 
0239     prePollStrings.push_back("");
0240     startDaemonStrings.push_back("");
0241     stopDaemonStrings.push_back("");
0242 }
0243 
0244 
0245 void ImportTab::removeImportProgram()
0246 {
0247     int selectedRow = deviceSelectionTable->currentRow();
0248     if (selectedRow >= 0) {
0249         prePollStrings.erase(prePollStrings.begin() + selectedRow);
0250         startDaemonStrings.erase(startDaemonStrings.begin() + selectedRow);
0251         stopDaemonStrings.erase(stopDaemonStrings.begin() + selectedRow);
0252         deviceSelectionTable->removeRow(selectedRow);
0253         contentsChanged(0, 0);
0254     }
0255 }
0256 
0257 
0258 void ImportTab::contentsChanged(int, int)
0259 {
0260     deviceSelectionTable->resizeColumnsToContents();
0261     int totalWidth = deviceSelectionTable->columnWidth(0) + deviceSelectionTable->columnWidth(1);
0262     int tableWidth = deviceSelectionTable->width() - 5;
0263     if ( totalWidth < tableWidth) {
0264         deviceSelectionTable->setColumnWidth( 1, tableWidth - deviceSelectionTable->columnWidth(0) );
0265     }
0266 }
0267 
0268 
0269 void ImportTab::activeCellChanged(int, int)
0270 {
0271     if ( grabberPreferences->isVisible() ) {
0272         changeSettings();
0273     }
0274 }
0275 
0276 
0277 void ImportTab::changeSettings()
0278 {
0279     int selected = deviceSelectionTable->currentRow();
0280     if (selected >= 0) {
0281         prePollEdit->setText(prePollStrings[selected]);
0282         startDaemonEdit->setText(startDaemonStrings[selected]);
0283         stopDaemonEdit->setText(stopDaemonStrings[selected]);
0284         grabberPreferences->show();
0285     }
0286 }
0287 
0288 
0289 void ImportTab::updatePrePollString(const QString &txt)
0290 {
0291     prePollStrings[deviceSelectionTable->currentRow()] = txt;
0292 }
0293 
0294 
0295 void ImportTab::updateStartDaemonString(const QString &txt)
0296 {
0297     startDaemonStrings[deviceSelectionTable->currentRow()] = txt;
0298 }
0299 
0300 
0301 void ImportTab::updatestopDaemonString(const QString &txt)
0302 {
0303     stopDaemonStrings[deviceSelectionTable->currentRow()] = txt;
0304 }
0305 
0306 
0307 void ImportTab::closeChangeBox()
0308 {
0309     grabberPreferences->hide();
0310     this->resize(minimumSize());
0311 }
0312 
0313 
0314 void ImportTab::retranslateStrings()
0315 {
0316     informationText->setHtml(
0317         "<p>" + tr("Below you can set which program/process Stopmotion should use "
0318         "for grabbing images from the selected device.") + "</p><p>" +
0319         tr("You should always use <b>$VIDEODEVICE</b> and <b>$IMAGEFILE</b> to represent "
0320         "the video device and the image file, respectively.") + "</p>");
0321 
0322     QStringList lst;
0323     lst << tr("Name") << tr("Description");
0324     deviceSelectionTable->setHorizontalHeaderLabels(lst);
0325 
0326     addButton->setText( tr("&Add") );
0327     removeButton->setText( tr("&Remove") );
0328     changeButton->setText( tr("&Edit") );
0329 
0330     grabberPreferences->setTitle( tr("Import device settings") );
0331     prePollLabel->setText( tr("Pre-poll command") );
0332     startDaemonLabel->setText( tr("Start daemon") );
0333     stopDaemonLabel->setText( tr("Stop daemon") );
0334 }