BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
UpdateNotifierWidget.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/mainwindow/UpdateNotifierWidget.cpp
6 //! @brief Implements class UpdateNotifierWidget
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
17 #include <QDebug>
18 #include <QDesktopServices>
19 #include <QHBoxLayout>
20 #include <QLabel>
21 #include <QSettings>
22 #include <QTimer>
23 #include <QUrl>
24 
25 namespace {
26 const QString yes = "yes";
27 const QString no = "no";
28 
29 QString update_question()
30 {
31  QString result = QString("Should BornAgain check for updates automatically? - "
32  " <a href=\"%1\">yes</a> /"
33  " <a href=\"%2\">no</a>")
34  .arg(yes, no);
35  return result;
36 }
37 } // namespace
38 
40  : QWidget(parent)
41  , m_updateNotifier(updateNotifier)
42  , m_updateLabel(new QLabel)
43  , m_check_for_updates(true)
44 {
45  auto layout = new QHBoxLayout();
46  layout->addWidget(m_updateLabel);
47  setLayout(layout);
48 
49  m_updateLabel->setContentsMargins(30, 10, 0, 0);
50  m_updateLabel->setOpenExternalLinks(false);
51  m_updateLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
52  m_updateLabel->setTextFormat(Qt::RichText);
53  m_updateLabel->setText("");
54 
55  m_updateNotifier = updateNotifier;
58 
59  connect(m_updateLabel, &QLabel::linkActivated, this, &UpdateNotifierWidget::onLinkActivated);
60 
62  m_updateLabel->setText(update_question());
63  m_updateLabel->setToolTip("This setting can be changed later in the main window "
64  "Settings menu.");
65  }
66 }
67 
68 //! Schedule check for updates if it was not done yet.
69 
70 void UpdateNotifierWidget::showEvent(QShowEvent* event)
71 {
72  QWidget::showEvent(event);
73  if (m_check_for_updates) {
74  m_check_for_updates = false;
75  QTimer::singleShot(1000, this, [&]() { m_updateNotifier->checkForUpdates(); });
76  }
77 }
78 
79 //! Updates label when notification is coming.
80 
82 {
83  m_updateLabel->setText(text);
84 }
85 
86 //! Processes mouse click on update notification label.
87 
88 void UpdateNotifierWidget::onLinkActivated(const QString& text)
89 {
90  if (text == yes) {
93  } else if (text == no) {
95  QTimer::singleShot(200, this, [&]() { m_updateLabel->setText(""); });
96  } else {
97  QDesktopServices::openUrl(QUrl(text));
98  }
99 }
Defines class UpdateNotifierWidget.
Implements class UpdateNotifier.
void onLinkActivated(const QString &text)
Processes mouse click on update notification label.
UpdateNotifierWidget(UpdateNotifier *updateNotifier, QWidget *parent=nullptr)
UpdateNotifier * m_updateNotifier
void showEvent(QShowEvent *event)
Schedule check for updates if it was not done yet.
void onUpdateNotification(const QString &text)
Updates label when notification is coming.
void setCheckUpdatesFlag(bool flag)
bool hasDefinedUpdatesFlag() const
Returns true if settings contain record about user choice for updates.
void onUpdateNotification(const QString &text)