1 #include "backlightwidget.h"
2 #include "ui_backlightwidget.h"
3 #include "ibacklight.h"
4 #include <QPainter>
5 #include <QPen>
6 #include <QBrush>
7 #include <QGuiApplication>
8 #include <QScreen>
9 #include <QDebug>
10 #include "systemmanager.h"
11
BacklightWidget(QWidget * parent)12 BacklightWidget::BacklightWidget(QWidget *parent) :
13 IWidget(parent),
14 ui(new Ui::BacklightWidget),
15 m_currentBacklight(NULL)
16 {
17 ui->setupUi(this);
18 }
19
~BacklightWidget()20 BacklightWidget::~BacklightWidget()
21 {
22 delete ui;
23 }
24
initUi()25 void BacklightWidget::initUi()
26 {
27
28 this->createColowWidget();
29
30 QGuiApplication *app = dynamic_cast<QGuiApplication*>(qApp);
31
32 if(!SystemManager::instance()->platformInfo().contains("noscreenname"))
33 {
34 foreach (QScreen *screen, app->screens()) {
35 ui->comboBox->addItem(screen->name());
36 }
37 }else{
38 foreach (Backlight *b, m_backlights) {
39 ui->comboBox->addItem(b->screenName());
40 }
41 }
42
43 ui->textBrowser->append("screen information: ");
44
45 connect(ui->comboBox, &QComboBox::currentTextChanged, this, [=](const QString &name){
46 foreach (Backlight *b, m_backlights) {
47
48 qDebug()<< b->screenName() << "/" << name;
49 if(b->screenName() == name){
50 if(m_currentBacklight != NULL && m_currentBacklight->screenName() != name)
51 {
52 m_currentBacklight->close();
53 }
54 m_currentBacklight = b;
55 ui->horizontalSlider->setEnabled(m_currentBacklight->open());
56 break;
57 }
58 }
59
60 foreach (QScreen *screen, app->screens()) {
61 QString screenName =screen->name();
62
63 if(SystemManager::instance()->platformInfo().contains("noscreenname"))
64 {
65 screenName = QString("%1x%2").arg(screen->size().width()).arg(screen->size().height());
66 }
67 if(screenName == name){
68 ui->textBrowser->clear();
69 ui->textBrowser->append(QString("屏幕个数: %1").arg(app->screens().count()));
70 ui->textBrowser->append(QString("屏幕像素分辨率: %1 X %2").arg( screen->size().width()).arg(screen->size().height()));
71 ui->textBrowser->append(QString("屏幕可用大小(像素为单位): %1 X %2").arg( screen->availableSize().width()).arg(screen->availableSize().height()));
72 ui->textBrowser->append(QString("每英寸的逻辑点或像素数: %1").arg( screen->logicalDotsPerInch()));
73 ui->textBrowser->append(QString("屏幕的颜色深度): %1").arg( screen->depth()));
74 ui->textBrowser->append(QString("垂直刷新率 Hz: %1").arg( screen->refreshRate()));
75 break;
76 }
77 }
78
79 });
80 QString screename;
81 if(SystemManager::instance()->platformInfo().contains("noscreenname"))
82 {
83 if(m_backlights.size() >0){
84 screename = m_backlights.at(0)->screenName();
85 }
86 }else{
87 screename =app->primaryScreen()->name();
88 }
89
90
91 ui->comboBox->setCurrentText(screename);
92
93 //更该下默认屏幕.
94 ui->comboBox->currentTextChanged(screename);
95
96 connect(app, &QGuiApplication::screenAdded, this, [=](QScreen *screen){
97 ui->textBrowser->append(QString("screen add: %1").arg(screen->name()));
98 int index = ui->comboBox->findText(screen->name());
99 if(index !=-1)
100 {
101 ui->comboBox->removeItem(index);
102 }
103 });
104
105 connect(app, &QGuiApplication::screenRemoved, this, [=](QScreen *screen){
106 ui->textBrowser->append(QString("screen remove: %1").arg(screen->name()));
107 int index = ui->comboBox->findText(screen->name());
108 if(index !=-1)
109 {
110 ui->comboBox->removeItem(index);
111 }
112 });
113
114 if(m_backlights.size() <=0)
115 {
116 ui->textBrowser->append("backling config is empty!");
117 }
118
119 connect(ui->horizontalSlider, &SliderWidget::valueChanged, this, [=](int value){
120 if(m_currentBacklight != NULL)
121 {
122 m_currentBacklight->setValue(value);
123 qCDebug(flapp)<<"set backligt value: "<< value;
124 system("sync");
125 }
126
127 ui->valueLbl->setText(QString::number(value));
128 });
129
130 if(m_currentBacklight != NULL)
131 {
132 int value = m_currentBacklight->getValue();
133 int min=0;
134 int max=0;
135 m_currentBacklight->getRange(min, max);
136 ui->minLbl->setText(QString::number(min));
137 ui->maxLbl->setText(QString::number(max));
138 ui->horizontalSlider->blockSignals(true);
139 ui->horizontalSlider->setRange(min, max);
140 ui->horizontalSlider->setValue(value);
141 ui->valueLbl->setText(QString::number(value));
142 ui->horizontalSlider->blockSignals(false);
143 }
144 }
145
createColowWidget()146 void BacklightWidget::createColowWidget()
147 {
148 int space = this->height()/32; ;
149 ui->verticalLayout_4->setSpacing(space);
150 auto createCircle = [=](QWidget *p, const QColor &color){
151 p->setFixedSize(38, 38);
152 p->setAutoFillBackground(true);
153 QPixmap pixmap(p->size());
154 QPainter painter(&pixmap);
155 painter.setPen(color);
156 painter.setRenderHint(QPainter::Antialiasing);
157 QRect rect = p->rect();
158 rect =rect.adjusted(1,1,-1,-1);
159 QPalette backPaalette= this->palette();
160 painter.fillRect(p->rect(), backPaalette.brush(QPalette::Background));
161 painter.setBrush(color);
162 painter.drawEllipse(rect);
163 QPalette palette = p->palette();
164 palette.setBrush(QPalette::Background, pixmap);
165 p->setPalette(palette);
166 };
167 createCircle(ui->redWidget, Qt::red);
168 createCircle(ui->greenWidget, Qt::green);
169 createCircle(ui->blueWidget, Qt::blue);
170 }
171
id()172 QString BacklightWidget::id()
173 {
174 return "screen";
175 }
176
loadData(const QDomElement & head,const QDomElement & body)177 void BacklightWidget::loadData(const QDomElement &head, const QDomElement &body)
178 {
179 Q_UNUSED(body)
180
181 Backlight *backlight;
182 for(int i=0; i<head.childNodes().size(); i++)
183 {
184 QDomElement e = head.childNodes().at(i).toElement();
185 QString devName = e.attribute("devName");
186 QString screenName = e.attribute("screenName");
187 int min = e.attribute("min").toInt();
188 int max = e.attribute("max").toInt();
189
190 if(SystemManager::instance()->platformInfo().contains("t507"))
191 {
192 backlight = new T507(devName, screenName, min, max);
193 }else{
194 backlight = new Backlight(devName,screenName, min, max);
195 }
196 m_backlights << backlight;
197 }
198 }
199