Rabbit Remote Control 0.0.30
Loading...
Searching...
No Matches
MainWindow.cpp
1// Author: Kang Lin <kl222@126.com>
2
3#include "MainWindow.h"
4#include "ui_MainWindow.h"
5
6#include "RabbitCommonDir.h"
7#include "RabbitCommonTools.h"
8#ifdef HAVE_ABOUT
9#include "DlgAbout.h"
10#endif
11
12#include <QSettings>
13#include <QDesktopServices>
14#include <QLoggingCategory>
15
16#ifdef HAVE_ICE
17 #include "Ice.h"
18#endif
19
20Q_DECLARE_LOGGING_CATEGORY(App)
21
22CMainWindow::CMainWindow(QWidget *parent) : QMainWindow(parent),
23 ui(new Ui::MainWindow),
24 m_bStart(false)
25{
26 ui->setupUi(this);
27
28 RabbitCommon::CTools::AddStyleMenu(ui->menuView);
29 ui->menuService->addMenu(RabbitCommon::CTools::GetLogMenu(ui->menuView));
30
31 Clean();
32 InitTab();
33}
34
35CMainWindow::~CMainWindow()
36{
37 delete ui;
38 foreach(auto plugin, m_Plugins.m_Plugins)
39 plugin->Stop();
40
41 foreach(auto service, m_Service)
42 if(service)
43 service->deleteLater();
44}
45
46void CMainWindow::on_pbCancel_clicked()
47{
48 qApp->quit();
49}
50
51void CMainWindow::on_pbSave_clicked()
52{
53 emit sigAccept();
54 foreach(auto service, m_Service)
55 {
56 service->SaveConfigure();
57 }
58#ifdef HAVE_ICE
59 QSettings set(RabbitCommon::CDir::Instance()->GetFileUserConfigure(), QSettings::IniFormat);
60 CICE::Instance()->GetParameter()->Save(set);
61#endif
62}
63
64int CMainWindow::InitTab()
65{
66 QWidget* pIce = nullptr;
67#ifdef HAVE_ICE
68 QSettings set(RabbitCommon::CDir::Instance()->GetFileUserConfigure(), QSettings::IniFormat);
69 CICE::Instance()->GetParameter()->Load(set);
70 pIce = CICE::Instance()->GetParameterWidget(ui->twConfigure);
71 if(pIce)
72 {
73 // parameter widget must has slotAccept()
74 bool check = connect(this, SIGNAL(sigAccept()), pIce, SLOT(slotAccept()));
75 if(!check)
76 {
77 qCritical(App) << "Class" << pIce->metaObject()->className()
78 << "must has slot slotAccept(), please add it";
79 }
80 Q_ASSERT(check);
81 int nIndex = ui->twConfigure->addTab(pIce, pIce->windowIcon(),
82 pIce->windowTitle());
83 if(-1 == nIndex)
84 qCritical(App) << "addTab ice fail";
85 }
86#endif
87
88 foreach(auto plugin, m_Plugins.m_Plugins)
89 {
90 CService* pService = plugin->NewService();
91 if(!pService) continue;
92 pService->LoadConfigure();
93 m_Service.push_back(pService);
94 QWidget* w = pService->GetParameterWidget(ui->twConfigure);
95 if(w)
96 {
97 // parameter widget must has slotAccept()
98 bool check = connect(this, SIGNAL(sigAccept()), w, SLOT(slotAccept()));
99 if(!check)
100 {
101 if(pIce)
102 qCritical(App) << "Class" << pIce->metaObject()->className()
103 << "must has slot slotAccept(), please add it";
104 }
105 Q_ASSERT(check);
106 int nIndex = ui->twConfigure->addTab(w, plugin->Icon(),
107 plugin->DisplayName());
108 if(-1 == nIndex)
109 qCritical(App) << "addTab fail";
110 }
111 }
112
113 return 0;
114}
115
116int CMainWindow::Clean()
117{
118 ui->twConfigure->clear();
119 return 0;
120}
121
122void CMainWindow::on_actionStart_triggered()
123{
124 foreach(auto plugin, m_Plugins.m_Plugins)
125 {
126 if(m_bStart)
127 plugin->Stop();
128 else
129 plugin->Start();
130 }
131
132#ifdef HAVE_ICE
133 if(m_bStart)
134 CICE::Instance()->slotStop();
135 else
136 CICE::Instance()->slotStart();
137#endif
138
139 if(m_bStart)
140 {
141 ui->actionStart->setIcon(QIcon::fromTheme("media-playback-start"));
142 ui->actionStart->setText(tr("Start"));
143 SetStatusText(tr("Start service"));
144 ui->actionStart->setChecked(false);
145 }
146 else
147 {
148 ui->actionStart->setIcon(QIcon::fromTheme("media-playback-stop"));
149 ui->actionStart->setText(tr("Stop"));
150 SetStatusText(tr("Stop service"));
151 ui->actionStart->setChecked(true);
152 }
153 m_bStart = !m_bStart;
154}
155
156int CMainWindow::SetStatusText(QString szText)
157{
158 ui->actionStart->setToolTip(szText);
159 ui->actionStart->setStatusTip(szText);
160 ui->actionStart->setWhatsThis(szText);
161 return 0;
162}
163
164void CMainWindow::on_actionAbout_triggered()
165{
166#ifdef HAVE_ABOUT
167 CDlgAbout *about = new CDlgAbout(this);
168 QIcon icon = QIcon::fromTheme("system-settings");
169 if(icon.isNull()) return;
170 auto sizeList = icon.availableSizes();
171 if(sizeList.isEmpty()) return;
172 QPixmap p = icon.pixmap(*sizeList.begin());
173 about->m_AppIcon = p.toImage();
174 about->m_szCopyrightStartTime = "2020";
175 about->m_szVersionRevision = RabbitRemoteControl_REVISION;
176 if(about->isHidden())
177 {
178#ifdef BUILD_QUIWidget
179 QUIWidget quiwidget;
180 quiwidget.setMainWidget(about);
181 quiwidget.setPixmap(QUIWidget::Lab_Ico, ":/image/App");
182 #if defined (Q_OS_ANDROID)
183 quiwidget.showMaximized();
184 #endif
185 quiwidget.exec();
186#else
187 #if defined (Q_OS_ANDROID)
188 about->showMaximized();
189 #endif
190 about->exec();
191#endif
192 }
193#endif
194}
195
196void CMainWindow::on_actionOpen_folder_triggered()
197{
198 QString szFolder;
199 QString szFile;
200 szFile = RabbitCommon::CDir::Instance()->GetFileUserConfigure();
201 QSettings set(szFile, QSettings::IniFormat);
202 szFolder = set.value("Configure/Folder",
203 RabbitCommon::CDir::Instance()->GetDirUserConfig()).toString();
204 if(szFolder.isEmpty()) return;
205 QDesktopServices::openUrl(QUrl::fromLocalFile(szFolder));
206}
static CICE * Instance()
Single instance.
Definition Ice.cpp:55
void sigAccept()
Notifies the plugin settings window that parameters have changed.
The service interface.
Definition Service.h:38
The MainWindow class.
Definition mainwindow.h:32