From: Mike Gelfand Date: Fri, 26 Dec 2014 18:41:47 +0000 (+0000) Subject: Refactor FreespaceLabel to make it possible for it to be created in Qt Designer X-Git-Tag: 2.90~289 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2ca4596faceeb8e042b01d7c64dd9b2cd86a2798;p=transmission Refactor FreespaceLabel to make it possible for it to be created in Qt Designer --- diff --git a/qt/freespace-label.cc b/qt/freespace-label.cc index 8bd76f2e0..cfbed19aa 100644 --- a/qt/freespace-label.cc +++ b/qt/freespace-label.cc @@ -19,24 +19,33 @@ namespace static const int INTERVAL_MSEC = 15000; } -FreespaceLabel::FreespaceLabel (Session & session, - const QString & path, - QWidget * parent): +FreespaceLabel::FreespaceLabel (QWidget * parent): QLabel (parent), - mySession (session), + mySession (nullptr), myTag (-1), myTimer (this) { - myTimer.setSingleShot (false); + myTimer.setSingleShot (true); myTimer.setInterval (INTERVAL_MSEC); - myTimer.start (); - connect (&myTimer, SIGNAL(timeout()), this, SLOT(onTimer())); + connect (&myTimer, SIGNAL (timeout ()), this, SLOT (onTimer ())); +} + +void +FreespaceLabel::setSession (Session& session) +{ + if (mySession == &session) + return; + + if (mySession != nullptr) + disconnect (mySession, nullptr, this, nullptr); - connect (&mySession, SIGNAL(executed(int64_t, QString, tr_variant *)), - this, SLOT(onSessionExecuted(int64_t, QString, tr_variant *))); + mySession = &session; - setPath (path); + connect (mySession, SIGNAL (executed (int64_t, QString, tr_variant *)), + this, SLOT (onSessionExecuted (int64_t, QString, tr_variant *))); + + onTimer (); } void @@ -53,7 +62,12 @@ FreespaceLabel::setPath (const QString& path) void FreespaceLabel::onTimer () { - const int64_t tag = mySession.getUniqueTag (); + myTimer.stop (); + + if (mySession == nullptr || myPath.isEmpty ()) + return; + + const int64_t tag = mySession->getUniqueTag (); const QByteArray myPathUtf8 = myPath.toUtf8 (); myTag = tag; @@ -63,7 +77,7 @@ FreespaceLabel::onTimer () tr_variantDictAddInt (&top, TR_KEY_tag, tag); tr_variant * args = tr_variantDictAddDict (&top, TR_KEY_arguments, 1); tr_variantDictAddStr (args, TR_KEY_path, myPathUtf8.constData()); - mySession.exec (&top); + mySession->exec (&top); tr_variantFree (&top); } @@ -91,4 +105,6 @@ FreespaceLabel::onSessionExecuted (int64_t tag, const QString& result, tr_varian tr_variantDictFindStr (arguments, TR_KEY_path, &path, &len); str = QString::fromUtf8 (path, len); setToolTip (str); + + myTimer.start (); } diff --git a/qt/freespace-label.h b/qt/freespace-label.h index 0aab72186..536251111 100644 --- a/qt/freespace-label.h +++ b/qt/freespace-label.h @@ -28,12 +28,14 @@ class FreespaceLabel: public QLabel Q_OBJECT public: - FreespaceLabel (Session&, const QString& path, QWidget *parent=0); + FreespaceLabel (QWidget * parent = 0); virtual ~FreespaceLabel () {} + + void setSession (Session& session); void setPath (const QString& folder); private: - Session& mySession; + Session * mySession; int64_t myTag; QString myPath; QTimer myTimer; diff --git a/qt/options.cc b/qt/options.cc index b27eb924a..f076262fd 100644 --- a/qt/options.cc +++ b/qt/options.cc @@ -109,7 +109,9 @@ Options::Options (Session& session, const Prefs& prefs, const AddData& addme, QW l = new QLabel (tr ("&Destination folder:")); layout->addWidget (l, ++row, 0, Qt::AlignLeft); const QString downloadDir (prefs.getString (Prefs::DOWNLOAD_DIR)); - myFreespaceLabel = new FreespaceLabel (mySession, downloadDir, this); + myFreespaceLabel = new FreespaceLabel (this); + myFreespaceLabel->setSession (mySession); + myFreespaceLabel->setPath (downloadDir); if (session.isLocal ()) { diff --git a/qt/prefs-dialog.cc b/qt/prefs-dialog.cc index 8dfc424d1..a81cd42f6 100644 --- a/qt/prefs-dialog.cc +++ b/qt/prefs-dialog.cc @@ -604,7 +604,9 @@ PrefsDialog::createDownloadingTab () hig->addRow (tr ("Save to &Location:"), b); const QString downloadDir (myPrefs.getString(Prefs::DOWNLOAD_DIR)); - l = myFreespaceLabel = new FreespaceLabel (mySession, downloadDir, this); + l = myFreespaceLabel = new FreespaceLabel (this); + myFreespaceLabel->setSession (mySession); + myFreespaceLabel->setPath (downloadDir); QHBoxLayout * h = new QHBoxLayout (); h->addStretch (1); h->addWidget (l);