"Fossies" - the Fresh Open Source Software Archive

Member "MP3Diags-unstable-1.5.01/src/SessionsDlgImpl.cpp" (10 Feb 2019, 18215 Bytes) of package /linux/privat/MP3Diags-unstable-1.5.01.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "SessionsDlgImpl.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.3.04_vs_1.5.01.

    1 /***************************************************************************
    2  *   MP3 Diags - diagnosis, repairs and tag editing for MP3 files          *
    3  *                                                                         *
    4  *   Copyright (C) 2009 by Marian Ciobanu                                  *
    5  *   ciobi@inbox.com                                                       *
    6  *                                                                         *
    7  *   This program is free software; you can redistribute it and/or modify  *
    8  *   it under the terms of the GNU General Public License version 2 as     *
    9  *   published by the Free Software Foundation.                            *
   10  *                                                                         *
   11  *   This program is distributed in the hope that it will be useful,       *
   12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
   13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
   14  *   GNU General Public License for more details.                          *
   15  *                                                                         *
   16  *   You should have received a copy of the GNU General Public License     *
   17  *   along with this program; if not, write to the                         *
   18  *   Free Software Foundation, Inc.,                                       *
   19  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
   20  ***************************************************************************/
   21 
   22 #include  <algorithm>
   23 
   24 #include  <QSettings>
   25 #include  <QHeaderView>
   26 #include  <QTimer>
   27 #include  <QFileDialog>
   28 #include  <QMessageBox>
   29 
   30 #include  "SessionsDlgImpl.h"
   31 
   32 #include  "Helpers.h"
   33 #include  "StoredSettings.h"
   34 #include  "CheckedDir.h"
   35 #include  "SessionEditorDlgImpl.h"
   36 #include  "OsFile.h"
   37 #include  "Widgets.h"
   38 #include  "Translation.h"
   39 
   40 using namespace std;
   41 
   42 //========================================================================================================================
   43 //========================================================================================================================
   44 //========================================================================================================================
   45 
   46 
   47 SessionsModel::SessionsModel(std::vector<std::string>& vstrSessions) : m_vstrSessions(vstrSessions)
   48 {
   49 }
   50 
   51 
   52 /*override*/ int SessionsModel::rowCount(const QModelIndex&) const { return cSize(m_vstrSessions); }
   53 
   54 /*override*/ QVariant SessionsModel::data(const QModelIndex& index, int nRole /* = Qt::DisplayRole*/) const
   55 {
   56 //LAST_STEP("SessionsModel::data()");
   57     if (!index.isValid()) { return QVariant(); }
   58     int i (index.row());
   59     //int j (index.column());
   60 
   61     // ttt2 perhaps Qt::ToolTipRole
   62     if (Qt::DisplayRole != nRole) { return QVariant(); }
   63     return toNativeSeparators(convStr(m_vstrSessions.at(i)));
   64 }
   65 
   66 
   67 /*override*/ QVariant SessionsModel::headerData(int nSection, Qt::Orientation eOrientation, int nRole /* = Qt::DisplayRole*/) const
   68 {
   69     if (nRole != Qt::DisplayRole) { return QVariant(); }
   70 
   71     if (Qt::Horizontal == eOrientation)
   72     {
   73         return tr("File name");
   74     }
   75 
   76     return nSection + 1;
   77 }
   78 
   79 
   80 //========================================================================================================================
   81 //========================================================================================================================
   82 //========================================================================================================================
   83 
   84 
   85 
   86 extern int CELL_HEIGHT;
   87 const QFont& getDefaultFont();
   88 
   89 SessionsDlgImpl::SessionsDlgImpl(QWidget* pParent) : QDialog(pParent, getMainWndFlags()), Ui::SessionsDlg(), m_sessionsModel(m_vstrSessions)
   90 {
   91     {
   92         QApplication::setFont(getDefaultFont());
   93         CELL_HEIGHT = QApplication::fontMetrics().height() + 3; //ttt2 hard-coded
   94     }
   95 
   96     setupUi(this);
   97 
   98     //vector<string> vstrSess;
   99     string strLast;
  100 
  101     //m_pSettings = SessionSettings::getGlobalSettings();
  102     GlobalSettings st;
  103     bool bOpenLast;
  104 
  105     st.loadSessions(m_vstrSessions, strLast, bOpenLast, m_strTempSessTempl, m_strDirSessTempl, m_strTranslation);
  106     m_pOpenLastCkB->setChecked(bOpenLast);
  107 
  108     m_pSessionsG->verticalHeader()->setSectionResizeMode(QHeaderView::Interactive);
  109     m_pSessionsG->verticalHeader()->setMinimumSectionSize(CELL_HEIGHT + 1); // ttt0 is this initialized before creating sessions? should it be?
  110     m_pSessionsG->verticalHeader()->setDefaultSectionSize(CELL_HEIGHT + 1);//*/
  111     m_pSessionsG->setModel(&m_sessionsModel);
  112     m_pSessionsG->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
  113     m_pSessionsG->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
  114     m_pSessionsG->horizontalHeader()->hide();
  115 
  116     m_pCheckedDirModel = new CheckedDirModel(this, CheckedDirModel::NOT_USER_CHECKABLE);
  117     m_pDirectoriesT->header()->hide();
  118     m_pDirectoriesT->setModel(m_pCheckedDirModel);
  119     m_pCheckedDirModel->setFilter(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::Drives);
  120     m_pCheckedDirModel->setSorting(QDir::IgnoreCase);
  121     m_pDirectoriesT->header()->setStretchLastSection(false); m_pDirectoriesT->header()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
  122 
  123     QPalette grayPalette (m_pDirectoriesT->palette());
  124     grayPalette.setColor(QPalette::Base, grayPalette.color(QPalette::Disabled, QPalette::Window));
  125     m_pDirectoriesT->setPalette(grayPalette);
  126 
  127     loadTemplates();
  128 
  129     //m_sessionsModel.emitLayoutChanged();
  130 
  131     connect(m_pSessionsG->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), this, SLOT(onCrtSessChanged()));
  132     connect(m_pSessionsG, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(onSessDoubleClicked(const QModelIndex&)));
  133     //m_pSessionsG->setCurrentIndex(m_sessionsModel.index(nLast, 0));
  134     if (strLast.empty() && !m_vstrSessions.empty())
  135     {
  136         strLast = m_vstrSessions.back();
  137     }
  138 
  139     if (!strLast.empty())
  140     {
  141         selectSession(strLast);
  142     }
  143 
  144     {
  145         int nWidth, nHeight;
  146         st.loadSessionsDlgSize(nWidth, nHeight);
  147         if (nWidth > 400 && nHeight > 400) { resize(nWidth, nHeight); }
  148     }
  149 
  150 
  151     { // language
  152         int nCrt (0);
  153         const vector<string>& vstrTranslations (TranslatorHandler::getGlobalTranslator().getTranslations());
  154         string strTmpTranslation (m_strTranslation); // !!! needed because on_m_pTranslationCbB_currentIndexChanged() will get triggered and change m_strTranslation
  155         for (int i = 0; i < cSize(vstrTranslations); ++i)
  156         {
  157             m_pTranslationCbB->addItem(convStr(TranslatorHandler::getLanguageInfo(vstrTranslations[i])));
  158             if (strTmpTranslation == vstrTranslations[i])
  159             {
  160                 nCrt = i;
  161             }
  162         }
  163         m_strTranslation = strTmpTranslation;
  164         m_pTranslationCbB->setCurrentIndex(nCrt);
  165     }
  166 
  167 
  168     { QAction* p (new QAction(this)); p->setShortcut(QKeySequence("F1")); connect(p, SIGNAL(triggered()), this, SLOT(onHelp())); addAction(p); }
  169 
  170     QTimer::singleShot(1, this, SLOT(onShow()));
  171 }
  172 
  173 SessionsDlgImpl::~SessionsDlgImpl()
  174 {
  175     GlobalSettings st;
  176     st.saveSessionsDlgSize(width(), height());
  177 
  178     {
  179         /*string strLast;
  180 
  181         //m_pSettings = SessionSettings::getGlobalSettings();
  182         //GlobalSettings st;
  183         vector<string> v;
  184         bool bOpenLast;
  185 
  186         string strTempSessTempl;
  187         string strDirSessTempl;
  188         string strTranslation;
  189         st.loadSessions(v, strLast, bOpenLast, strTempSessTempl, strDirSessTempl, strTranslation); //ttt0 what's the point of this? no vars are used*/
  190         saveTemplates();
  191         st.saveSessions(m_vstrSessions, getCrtSession(), m_pOpenLastCkB->isChecked(), m_strTempSessTempl, m_strDirSessTempl, m_strTranslation, GlobalSettings::LOAD_EXTERNAL_CHANGES);
  192     }
  193 }
  194 
  195 // ttt2 generic inconsistency in what is saved depending on the user clicking the "x" button, pressing ESC, clicking other button ...
  196 
  197 
  198 // sets up the combo boxes with temp/folder session templates based on m_vstrSessions, m_strTempSessTempl, and m_strDirSessTempl
  199 void SessionsDlgImpl::loadTemplates()
  200 {
  201     m_pTempSessionCbB->clear(); m_pTempSessionCbB->addItem(tr("<last session>"));
  202     m_pDirSessionCbB->clear(); m_pDirSessionCbB->addItem(tr("<last session>"));
  203 
  204     for (int i = 0; i < cSize(m_vstrSessions); ++i)
  205     {
  206         m_pTempSessionCbB->addItem(toNativeSeparators(convStr(m_vstrSessions[i])));
  207         if (m_strTempSessTempl == m_vstrSessions[i])
  208         {
  209             m_pTempSessionCbB->setCurrentIndex(m_pTempSessionCbB->count() - 1);
  210         }
  211 
  212         m_pDirSessionCbB->addItem(toNativeSeparators(convStr(m_vstrSessions[i])));
  213         if (m_strDirSessTempl == m_vstrSessions[i])
  214         {
  215             m_pDirSessionCbB->setCurrentIndex(m_pDirSessionCbB->count() - 1);
  216         }
  217     }
  218 }
  219 
  220 
  221 // sets c and m_strDirSessTempl based on the current items in the combo boxes
  222 void SessionsDlgImpl::saveTemplates()
  223 {
  224     m_strTempSessTempl.clear();
  225     if (m_pTempSessionCbB->currentIndex() > 0)
  226     {
  227         m_strTempSessTempl = fromNativeSeparators(convStr(m_pTempSessionCbB->currentText()));
  228     }
  229 
  230     m_strDirSessTempl.clear();
  231     if (m_pDirSessionCbB->currentIndex() > 0)
  232     {
  233         m_strDirSessTempl = fromNativeSeparators(convStr(m_pDirSessionCbB->currentText()));
  234     }
  235 }
  236 
  237 
  238 void SessionsDlgImpl::onShow()
  239 {
  240     m_pCheckedDirModel->expandNodes(m_pDirectoriesT);
  241 }
  242 
  243 
  244 string SessionsDlgImpl::run()
  245 {
  246     if (QDialog::Accepted != exec()) { return ""; }
  247 
  248     return getCrtSession();
  249 }
  250 
  251 void GlobalSettings::saveSessionsDlgSize(int nWidth, int nHeight)
  252 {
  253     m_pSettings->setValue("sessions/width", nWidth);
  254     m_pSettings->setValue("sessions/height", nHeight);
  255 }
  256 
  257 void GlobalSettings::loadSessionsDlgSize(int& nWidth, int& nHeight) const
  258 {
  259     nWidth = m_pSettings->value("sessions/width").toInt();
  260     nHeight = m_pSettings->value("sessions/height").toInt();
  261 }
  262 
  263 
  264 void GlobalSettings::saveSessionEdtSize(int nWidth, int nHeight)
  265 {
  266     m_pSettings->setValue("sessionEditor/width", nWidth);
  267     m_pSettings->setValue("sessionEditor/height", nHeight);
  268 }
  269 
  270 void GlobalSettings::loadSessionEdtSize(int& nWidth, int& nHeight) const
  271 {
  272     nWidth = m_pSettings->value("sessionEditor/width").toInt();
  273     nHeight = m_pSettings->value("sessionEditor/height").toInt();
  274 }
  275 
  276 
  277 
  278 void SessionsDlgImpl::onCrtSessChanged()
  279 {
  280     int i (m_pSessionsG->selectionModel()->currentIndex().row());
  281     int n (cSize(m_vstrSessions));
  282     if (i < 0 || i >= n)
  283     {
  284         return;
  285     }
  286 
  287     SessionSettings st (m_vstrSessions[i]);
  288     vector<string> vstrCheckedDirs, vstrUncheckedDirs;
  289 
  290     bool bOk (st.loadDirs(vstrCheckedDirs, vstrUncheckedDirs));
  291     if (!bOk)
  292     { //ttt2 some warning
  293     }
  294 
  295     m_pCheckedDirModel->setDirs(vstrCheckedDirs, vstrUncheckedDirs, m_pDirectoriesT);
  296 }
  297 
  298 
  299 void SessionsDlgImpl::selectSession(const string& strLast)
  300 {
  301     vector<string>::iterator it (std::find(m_vstrSessions.begin(), m_vstrSessions.end(), strLast));
  302     CB_ASSERT (m_vstrSessions.end() != it);
  303     int k (it - m_vstrSessions.begin());
  304     m_sessionsModel.emitLayoutChanged();
  305     m_pSessionsG->setCurrentIndex(m_sessionsModel.index(k, 0));
  306     onCrtSessChanged();
  307 }
  308 
  309 
  310 
  311 
  312 string SessionsDlgImpl::getCrtSession() const
  313 {
  314     int i (m_pSessionsG->currentIndex().row());
  315     if (i < 0 || i >= cSize(m_vstrSessions)) { return ""; }
  316     return m_vstrSessions[i];
  317 }
  318 
  319 
  320 
  321 string SessionsDlgImpl::getCrtSessionDir() const
  322 {
  323     string s (getCrtSession());
  324     if (!s.empty())
  325     {
  326         s = getParent(s);
  327     }
  328     return s;
  329 }
  330 
  331 void SessionsDlgImpl::addSession(const std::string& strSession)
  332 {
  333     int n (cSize(m_vstrSessions));
  334     for (int i = 0; i < n; ++i)
  335     {
  336         if (m_vstrSessions[i] == strSession) //ttt2 ignore case on windows
  337         {
  338             m_vstrSessions.erase(m_vstrSessions.begin() + i);
  339             --n;
  340             break;
  341         }
  342     }
  343     m_vstrSessions.push_back(strSession);
  344 
  345     selectSession(strSession);
  346 
  347     GlobalSettings st;
  348     loadTemplates();
  349     saveTemplates();
  350     st.saveSessions(m_vstrSessions, strSession, m_pOpenLastCkB->isChecked(), m_strTempSessTempl, m_strDirSessTempl, m_strTranslation, GlobalSettings::LOAD_EXTERNAL_CHANGES);
  351 }
  352 
  353 
  354 void SessionsDlgImpl::on_m_pNewB_clicked()
  355 {
  356     string strSession;
  357     {
  358         SessionEditorDlgImpl dlg (this, getCrtSessionDir(), SessionEditorDlgImpl::NOT_FIRST_TIME, m_strTranslation);
  359         strSession = dlg.run();
  360         if (strSession.empty())
  361         {
  362             return;
  363         }
  364     }
  365 
  366     addSession(strSession);
  367 }
  368 
  369 
  370 void SessionsDlgImpl::on_m_pEditB_clicked()
  371 {
  372     string strSession (getCrtSession());
  373     if (strSession.empty())
  374     {
  375         return;
  376     }
  377 
  378     SessionEditorDlgImpl dlg (this, strSession);
  379     dlg.run();
  380 
  381     //m_sessionsModel.emitLayoutChanged();
  382     onCrtSessChanged();
  383 }
  384 
  385 
  386 void SessionsDlgImpl::removeCrtSession()
  387 {
  388     int k (m_pSessionsG->currentIndex().row());
  389     int n (cSize(m_vstrSessions));
  390     if (k < 0 || k >= n) { return; }
  391     m_vstrSessions.erase(m_vstrSessions.begin() + k);
  392 
  393     //m_sessionsModel.emitLayoutChanged();
  394 
  395     if (k == n - 1)
  396     {
  397         --k;
  398     }
  399 
  400     string strCrtSess;
  401     if (k >= 0)
  402     {
  403         strCrtSess = m_vstrSessions[k];
  404         selectSession(strCrtSess);
  405     }
  406     else
  407     {
  408         vector<string> v;
  409         m_pCheckedDirModel->setDirs(v, v, m_pDirectoriesT); // to uncheck all dirs
  410     }
  411 
  412     GlobalSettings st;
  413     loadTemplates();
  414     saveTemplates();
  415     st.saveSessions(m_vstrSessions, strCrtSess, m_pOpenLastCkB->isChecked(), m_strTempSessTempl, m_strDirSessTempl, m_strTranslation, GlobalSettings::IGNORE_EXTERNAL_CHANGES);
  416 }
  417 
  418 
  419 void SessionsDlgImpl::on_m_pEraseB_clicked()
  420 {
  421     if (m_vstrSessions.empty()) { return; }
  422     if (0 != showMessage(this, QMessageBox::Question, 1, 1, tr("Confirm"), tr("Do you really want to erase the current session?"), tr("Erase"), tr("Cancel"))) { return; }
  423     string s (getCrtSession());
  424     removeCrtSession();
  425 
  426     try
  427     {
  428         //ttt0 use eraseFiles()
  429         deleteFile(s);
  430         deleteFile(SessionEditorDlgImpl::getDataFileName(s));
  431         if (fileExists(SessionEditorDlgImpl::getLogFileName(s)))
  432         {
  433             deleteFile(SessionEditorDlgImpl::getLogFileName(s));
  434         }
  435     }
  436     catch (const std::bad_alloc&) { throw; }
  437     catch (...) //ttt2 use specific exceptions
  438     {
  439         showCritical(this, tr("Error"), tr("Failed to remove the data files associated with this session")); // maybe the files were already deleted ...
  440         return;
  441     }
  442 }
  443 
  444 
  445 //ttt2 perhaps: when choosing dirs show them in the title bar or a label
  446 void SessionsDlgImpl::on_m_pSaveAsB_clicked()
  447 {
  448     if (m_vstrSessions.empty()) { return; }
  449     //string s (getCrtSession());
  450 
  451     QFileDialog dlg (this, tr("Save session as ..."), convStr(getCrtSessionDir()), SessionEditorDlgImpl::tr("MP3 Diags session files (*%1)").arg(SessionEditorDlgImpl::SESS_EXT));
  452     dlg.setAcceptMode(QFileDialog::AcceptSave);
  453 
  454     if (QDialog::Accepted != dlg.exec()) { return; }
  455 
  456     QStringList fileNames (dlg.selectedFiles());
  457     if (1 != fileNames.size()) { return; }
  458 
  459     string s (convStr(fileNames.first()));
  460     if (!endsWith(s, SessionEditorDlgImpl::SESS_EXT)) { s += SessionEditorDlgImpl::SESS_EXT; }
  461 
  462     string strCrt (getCrtSession());
  463     if (s == strCrt) { return; }
  464 
  465     try
  466     {
  467         copyFile2(strCrt, s);
  468         copyFile2(SessionEditorDlgImpl::getDataFileName(strCrt), SessionEditorDlgImpl::getDataFileName(s));
  469         if (fileExists(SessionEditorDlgImpl::getLogFileName(strCrt)))
  470         {
  471             copyFile2(SessionEditorDlgImpl::getLogFileName(strCrt), SessionEditorDlgImpl::getLogFileName(s));
  472         }
  473     }
  474     catch (const std::bad_alloc&) { throw; }
  475     catch (...)
  476     { //ttt2 show errors
  477     }
  478     addSession(s);
  479 }
  480 
  481 
  482 void SessionsDlgImpl::on_m_pHideB_clicked()
  483 {
  484     if (m_vstrSessions.empty()) { return; }
  485     if (0 != showMessage(this, QMessageBox::Question, 1, 1, tr("Confirm"), tr("Do you really want to hide the current session?"), tr("Hide"), tr("Cancel"))) { return; }
  486     removeCrtSession();
  487 }
  488 
  489 
  490 void SessionsDlgImpl::on_m_pLoadB_clicked()
  491 {
  492     //ttt0 doesn't open in the "right" place; should be either (localized) Documents or the the dir of the last session, but opens in parent dirs; perhaps related to changelist 831
  493     QFileDialog dlg (this, tr("Choose a session file"), convStr(getCrtSessionDir()), SessionEditorDlgImpl::tr("MP3 Diags session files (*%1)").arg(SessionEditorDlgImpl::SESS_EXT)); //ttt0 add ".ini", for import from older versions
  494     dlg.setAcceptMode(QFileDialog::AcceptOpen);
  495 
  496     if (QDialog::Accepted != dlg.exec()) { return; }
  497 
  498     QStringList fileNames (dlg.selectedFiles());
  499     if (1 != fileNames.size()) { return; }
  500 
  501     string s (convStr(fileNames.first()));
  502     CB_ASSERT (endsWith(s, SessionEditorDlgImpl::SESS_EXT));
  503 
  504     for (int i = 0, n = cSize(m_vstrSessions); i < n; ++i)
  505     {
  506         if (m_vstrSessions[i] == s)
  507         {
  508             showCritical(this, tr("Error"), tr("The session named \"%1\" is already part of the session list").arg(toNativeSeparators(convStr(s))));
  509             return;
  510         }
  511     }
  512 
  513     addSession(s);
  514 }
  515 
  516 
  517 void SessionsDlgImpl::on_m_pOpenB_clicked()
  518 {
  519     if (getCrtSession().empty())
  520     {
  521         showCritical(this, tr("Error"), tr("The session list is empty. You must create a new session or load an existing one."));
  522         return;
  523     }
  524     accept();
  525 }
  526 
  527 
  528 void SessionsDlgImpl::on_m_pCloseB_clicked() // ttt0 redo screenshots Cancel->Close
  529 {
  530     reject();
  531 }
  532 
  533 
  534 void SessionsDlgImpl::on_m_pTranslationCbB_currentIndexChanged(int)
  535 {
  536     const vector<string>& vstrTranslations (TranslatorHandler::getGlobalTranslator().getTranslations());
  537     m_strTranslation = vstrTranslations[m_pTranslationCbB->currentIndex()];
  538 
  539     TranslatorHandler::getGlobalTranslator().setTranslation(m_strTranslation);
  540     retranslateUi(this);
  541     loadTemplates(); // !!! to retranslate "<last session>"
  542 }
  543 
  544 void SessionsDlgImpl::onSessDoubleClicked(const QModelIndex& index)
  545 {
  546     selectSession(m_vstrSessions.at(index.row()));
  547     on_m_pOpenB_clicked();
  548 }
  549 
  550 void SessionsDlgImpl::onHelp()
  551 {
  552     openHelp("310_advanced.html");
  553 }
  554