Skip to content

Commit

Permalink
modift a button
Browse files Browse the repository at this point in the history
  • Loading branch information
georgezzzh committed Nov 21, 2020
1 parent addfbbe commit bf17aa6
Show file tree
Hide file tree
Showing 14 changed files with 294 additions and 169 deletions.
6 changes: 6 additions & 0 deletions Lyrics/Lyrics.pro
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,32 @@ DEFINES += QT_DEPRECATED_WARNINGS

SOURCES += \
applebutton.cpp \
bgcolor_btn.cpp \
client.cpp \
desklyric.cpp \
lrc_button.cpp \
main.cpp \
mainwindow.cpp \
mylabel.cpp \
mytextbrowser.cpp \
neteasecloudmusicclient.cpp \
qqmusicclient.cpp \
setstruct.cpp \
settings.cpp \
spotify.cpp

HEADERS += \
applebutton.h \
bgcolor_btn.h \
client.h \
desklyric.h \
lrc_button.h \
mainwindow.h \
mylabel.h \
mytextbrowser.h \
neteasecloudmusicclient.h \
qqmusicclient.h \
setstruct.h \
settings.h \
spotify.h

Expand Down
4 changes: 2 additions & 2 deletions Lyrics/Translation_EN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@
<message>
<location filename="mainwindow.ui" line="60"/>
<source>&lt;&lt;</source>
<translation type="unfinished">&lt;&lt;</translation>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="80"/>
<source>&gt;&gt;</source>
<translation type="unfinished">&gt;&gt;</translation>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="100"/>
Expand Down
25 changes: 21 additions & 4 deletions Lyrics/applebutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <QPainter>
#include <QEvent>
#include <QMouseEvent>
#include "setstruct.h"
void AppleButton::paintEvent(QPaintEvent* ev)
{
QPainter* painter=new QPainter(this);
Expand Down Expand Up @@ -35,15 +36,16 @@ void AppleButton::paintEvent(QPaintEvent* ev)
int m_space = 3; //george added
QColor m_textColor = QColor("white");
int sliderWidth = qMin(height(), width()) - m_space * 2 - 5;
cout<<"current mcheck,"<<m_checked<<",mode"<<mode;
if (m_checked){
QRect textRect(0, 0, width() - sliderWidth, height());
painter->setPen(QPen(m_textColor));
QString m_textOn = tr("开启");
QString m_textOn =this->m_textOn;
painter->drawText(textRect, Qt::AlignCenter, m_textOn);
} else {
QRect textRect(sliderWidth, 0, width() - sliderWidth, height());
painter->setPen(QPen(m_textColor));
QString m_textOff = tr("关闭");
QString m_textOff = this->m_textOff;
painter->drawText(textRect, Qt::AlignCenter, m_textOff);
}
drawSlider();
Expand All @@ -66,12 +68,27 @@ void AppleButton::drawSlider()
void AppleButton::mousePressEvent(QMouseEvent* ev)
{
m_checked = !m_checked;
cout<<"clicked,"<<this->mode;
if(this->mode==MODE::LRC_BTN){
emit deskLrcBtnClick(m_checked);
}else if(this->mode==MODE::BG_COLOR_BTN){
emit bgColorBtnClick(m_checked);
}
this->update();
emit appleClick(m_checked);

}
AppleButton::AppleButton(QWidget *parent) : QPushButton(parent)
AppleButton::AppleButton(MODE mode,QWidget *parent) : QPushButton(parent)
{
qDebug()<<"appleMock pushbutton";
this->mode = mode;
this->m_checked = false;
this->m_textOn=QString("On");
this->m_textOff=QString("Off");
SetStruct set;
if(set.lang=="English"){
this->m_textOn=QString("On");
this->m_textOff=QString("Off");
}
this->setFixedSize(80,30);
this->update();
}
10 changes: 8 additions & 2 deletions Lyrics/applebutton.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@

#include <QWidget>
#include <QPushButton>
#define cout qDebug().noquote()<<"["<<__FILE__<<":"<<__LINE__<<"]: "
enum MODE{BG_COLOR_BTN,LRC_BTN};
class AppleButton : public QPushButton
{
Q_OBJECT
public:
explicit AppleButton(QWidget *parent = nullptr);
explicit AppleButton(MODE mode,QWidget *parent = nullptr);
void setCheck(bool ck){this->m_checked=ck;}
protected:
void paintEvent(QPaintEvent *) override;
void mousePressEvent(QMouseEvent*) override;
private:
bool m_checked;
MODE mode;
QString m_textOn;
QString m_textOff;
void drawSlider();
signals:
void appleClick(bool);
void deskLrcBtnClick(bool);
void bgColorBtnClick(bool);

};

Expand Down
7 changes: 7 additions & 0 deletions Lyrics/bgcolor_btn.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "bgcolor_btn.h"
#include "applebutton.h"

BgColor_btn::BgColor_btn(QWidget *parent) : AppleButton(BG_COLOR_BTN,parent)
{

}
17 changes: 17 additions & 0 deletions Lyrics/bgcolor_btn.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef BGCOLOR_BTN_H
#define BGCOLOR_BTN_H

#include <QWidget>
#include "applebutton.h"

class BgColor_btn : public AppleButton
{
Q_OBJECT
public:
explicit BgColor_btn(QWidget *parent = nullptr);

signals:

};

#endif // BGCOLOR_BTN_H
6 changes: 6 additions & 0 deletions Lyrics/lrc_button.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "lrc_button.h"
#include "applebutton.h"
Lrc_button::Lrc_button(QWidget *parent) : AppleButton(LRC_BTN,parent)
{

}
18 changes: 18 additions & 0 deletions Lyrics/lrc_button.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef LRC_BUTTON_H
#define LRC_BUTTON_H

#include "applebutton.h"
#include <QWidget>
#include <QPushButton>

class Lrc_button : public AppleButton
{
Q_OBJECT
public:
explicit Lrc_button(QWidget *parent = nullptr);

signals:

};

#endif // LRC_BUTTON_H
9 changes: 7 additions & 2 deletions Lyrics/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,16 @@ void MainWindow::configChange()
this->deskLyric->show();
ui->desklyricFlag->setStyleSheet("QLabel{color:#1DB954;}QLabel:hover{color:#1DB954;}");
}

//update set page
cout<<"set change mainwindow";
setStruct->writeConfigFile();
//reset set UI
this->set->resetCombox();
//update lyric
this->resetLyricDisplay();
}
void MainWindow::langInit()
{

QLocale local;
//加载翻译文件
this->trans = new QTranslator(this);
Expand Down
68 changes: 68 additions & 0 deletions Lyrics/setstruct.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <QFile>
#include <QDir>
#include <QStandardPaths>
#include <QJsonObject>
#include <QJsonDocument>
#include <QDebug>
#include "setstruct.h"
#define cout qDebug().noquote()<<"["<<__FILE__<<":"<<__LINE__<<"]: "

void SetStruct::setAttributes(QString bgColor,QString fontFamily,QString fontSize,QString lang){
this->bgColor = bgColor;
this->fontFamily = fontFamily;
this->fontSize = fontSize;
this->lang = lang;

}
void SetStruct::setDeskLrc(QString deskLrcStatus,QString deskLrcOri){
this->deskLrcOri = deskLrcOri;
this->deskLrcStatus = deskLrcStatus;
}
void SetStruct::initConfigFile(){
QString appDataPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
QDir appDir(appDataPath);
if(!appDir.exists()){
//创建父级目录
appDir.mkdir(appDataPath);
}
this->configName = appDataPath +"/config.json";
cout<<"configName:"<<configName;
//读取配置
QFile file(configName);
if(!file.exists()) {
qDebug()<<"config file does not exist";
return;
}
file.open(QIODevice::ReadOnly);
QString config=file.readAll();
QJsonDocument document = QJsonDocument::fromJson(config.toUtf8());
QJsonObject jsonObject = document.object();
QString lang = jsonObject.value("lang").toString();
QString fontSize = jsonObject.value("fontSize").toString();
QString fontFamily = jsonObject.value("fontFamily").toString();
QString bgColor = jsonObject.value("bgColor").toString();
QString deskLrcOri = jsonObject.value("deskLrcOri").toString();
QString deskLrcStatus = jsonObject.value("deskLrcStatus").toString();
this->setAttributes(bgColor,fontFamily,fontSize,lang);
this->setDeskLrc(deskLrcStatus,deskLrcOri);
}
void SetStruct::writeConfigFile(){
cout<<"modify changeset file";
QJsonObject jsonObject;
jsonObject.insert("fontSize",this->fontSize);
jsonObject.insert("fontFamily",this->fontFamily);
jsonObject.insert("lang",this->lang);
jsonObject.insert("bgColor",this->bgColor);
jsonObject.insert("deskLrcStatus",this->deskLrcStatus);
jsonObject.insert("deskLrcOri",this->deskLrcOri);
QJsonDocument jsonDoc;
jsonDoc.setObject(jsonObject);
QFile file(this->configName);
if(file.exists()){
file.remove();
}
if(file.open(QIODevice::WriteOnly)){
file.write(jsonDoc.toJson());
}
file.close();
}
35 changes: 35 additions & 0 deletions Lyrics/setstruct.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef SETSTRUCT_H
#define SETSTRUCT_H
#include <QString>

class SetStruct
{
private:
QString configName;
public:
QString bgColor;
QString fontFamily;
QString fontSize;
//简体中文,English
QString lang;
QString deskLrcStatus;
QString deskLrcOri;
//语言不设置预置项
SetStruct(QString bgColor="white",QString fontFamily="Arial",
QString fontSize="20",QString lang="") {
this->bgColor = bgColor;
this->fontFamily = fontFamily;
this->fontSize = fontSize;
this->lang = lang;
this->deskLrcStatus = "off";
this->deskLrcOri = "HORIZONTAL";
initConfigFile();
}
void setDeskLrc(QString deskLrcStatus = "off",QString deskLrcOri = "HORIZONTAL");
void setAttributes(QString bgColor,QString fontFamily,QString fontSize,QString lang);
void initConfigFile();
void writeConfigFile();

};

#endif // SETSTRUCT_H
Loading

0 comments on commit bf17aa6

Please sign in to comment.