Skip to content
Permalink
Browse files

jsw

  • Loading branch information...
taran
taran committed Apr 15, 2019
1 parent fd13cb2 commit 464774e62312cf6d0a1f2e2c24185edd16ea40b7
Showing with 80 additions and 49 deletions.
  1. +29 −15 jswidgets/SWidget.cpp
  2. +7 −5 jswidgets/SWidget.h
  3. +41 −26 jswidgets/SWidgetList.cpp
  4. +3 −3 jswidgets/SWidgetList.h
@@ -13,26 +13,29 @@
#include <QTimer>
#include <QDebug>

SWidget::SWidget(QWidget *parent, QString fileName) {
m_sat = 0; //TODO nullptr, fileName - const
m_loc = 0;
m_time = 0;
SWidget::SWidget(QWidget *parent, const QString &fileName) : QWidget(parent) {
m_sat = nullptr;
m_loc = nullptr;
m_time = nullptr;
m_maskColor = Qt::magenta;
m_fillColor = Qt::black;
this->resize(64, 32);

QFile file(fileName);
if (!file.open(QFile::ReadOnly)) {
if (!file.open(QFile::ReadOnly | QFile::Text)) {
qWarning() << "Error script file open: " << file.errorString();
return;
}
QTextStream stream(&file);
script = stream.readAll();
file.close();

QJSValue scriptCanvas = engine.newQObject(this);
engine.globalObject().setProperty("canvas", scriptCanvas);

setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint);
setCursor(Qt::OpenHandCursor);
this->setParent(parent);

pixmap = QPixmap(width(), height());
pixmap.fill(Qt::black);
if (!canvas.begin(&pixmap)) {
@@ -41,13 +44,18 @@ SWidget::SWidget(QWidget *parent, QString fileName) {

engine.evaluate(script);
m_title = engine.globalObject().property("title").toString();
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(onTimer()));
timer->setInterval(1000);
timer->start();

m_interval = 1000;
m_timer = startTimer(m_interval);
this->onTimer();
}

void SWidget::timerEvent(QTimerEvent *event) {
if (event->timerId() == m_timer) {
onTimer();
}
}

void SWidget::paintEvent(QPaintEvent *event) {
Q_UNUSED(event);
QPainter painter(this);
@@ -88,11 +96,11 @@ void SWidget::onTimer() {
repaint();
}

void SWidget::drawText(int x, int y, QString text) {
void SWidget::drawText(int x, int y, const QString &text) {
canvas.drawText(x, y, text);
}

int SWidget::textWidth(QString text) {
int SWidget::textWidth(const QString &text) {
return canvas.fontMetrics().width(text);
}

@@ -149,7 +157,10 @@ void SWidget::setPos(int x, int y) {
}

void SWidget::setInterval(int value) {
timer->setInterval(value);
m_interval = value;
if (m_timer >= 0) {
activate(true);
}
}

void SWidget::setMaskColor(int value) {
@@ -189,12 +200,15 @@ void SWidget::set(Satellite *sat, Location *loc, double *time) {
}

void SWidget::activate(bool state) {
if (m_timer >= 0) {
killTimer(m_timer);
m_timer = -1;
}
if (state) {
timer->start();
m_timer = startTimer(m_interval);
show();
}
else {
timer->stop();
hide();
}
}
@@ -18,22 +18,22 @@
class SWidget : public QWidget {
Q_OBJECT
public:
SWidget(QWidget *parent = 0, QString fileName = "");
SWidget(QWidget* parent = nullptr, const QString &fileName = "");
virtual ~SWidget();
void set(Satellite *sat, Location *loc, double *time);
inline QString title() { return m_title; }
void activate(bool state = true);

public slots:
void drawText(int x, int y, QString text);
void drawText(int x, int y, const QString &text);
void fill(int value);
void setPen(int value);
void setFont(QString name, int pointSize, int weight, bool italic);
void setSize(int w, int h);
void setPos(int x, int y);
void setInterval(int value);
void setMaskColor(int value);
int textWidth(QString text);
int textWidth(const QString &text);
int fontHeight();

protected:
@@ -46,15 +46,17 @@ public slots:
QPixmap pixmap;
QPainter canvas;
QColor m_maskColor, m_fillColor;
QTimer *timer;
int m_timer;
int m_interval;
QString script;
QString m_title;
QJSEngine engine;
Satellite *m_sat;
Location *m_loc;
double *m_time;
void setVars();

void timerEvent(QTimerEvent *event);

private slots:
void onTimer();
};
@@ -12,67 +12,74 @@

SWidgetList::SWidgetList(QWidget *parent) : QWidget(parent) {
ui.setupUi(this);
clear();
connect(ui.btnSave, SIGNAL(clicked()), this, SLOT(save()));
connect(ui.listView, SIGNAL(clicked(const QModelIndex &)), this, SLOT(changeState(const QModelIndex &)));
connect(ui.listView, SIGNAL(clicked(const QModelIndex &)),
this, SLOT(changeState(const QModelIndex &)));
}

SWidgetList::~SWidgetList() {

}

void SWidgetList::init(QWidget *desktop, QString path) {
void SWidgetList::init(QWidget *desktop, const QString &path) {
QStandardItemModel *model = new QStandardItemModel();
QDir dir(path);
QStringList list = dir.entryList(QDir::Files);
QFileInfo info;
SWidget *sWidget;
QStandardItem *item;
for (int i = 0; i < list.count(); i++) {
info.setFile(list.at(i));

for (const auto& line : list) {
info.setFile(line);
if (info.suffix() == "js") {
sWidget = new SWidget(desktop, dir.filePath(list.at(i)));
SWidget *sWidget = new SWidget(desktop, dir.filePath(line));
this->append(sWidget);
item = new QStandardItem(sWidget->title());
QStandardItem *item = new QStandardItem(sWidget->title());
model->appendRow(item);
item->setCheckable(true);
item->setCheckState(Qt::Checked);
}
}

ui.listView->setModel(model);
stateFile = new QFile(dir.filePath("state.lst"));
stateFile.setFileName(dir.filePath("state.lst"));
load();
}

void SWidgetList::setVars(Satellite *sat, Location *loc, double *time) {
for (int i = 0; i < this->count(); i++)
this->at(i)->set(sat, loc, time);
for (const auto& widget : (*this)) {
widget->set(sat, loc, time);
}
}

void SWidgetList::save() {
if (!stateFile->open(QFile::WriteOnly | QFile::Text)) {
puts(stateFile->errorString().toLocal8Bit().data());
if (!stateFile.open(QFile::WriteOnly | QFile::Text)) {
qWarning() << stateFile.errorString();
return;
}
QStandardItemModel *model = (QStandardItemModel *)ui.listView->model();
QString line = "";

for (int i = 0; i < model->rowCount(); i++) {
if (model->item(i)->checkState() == Qt::Checked) line.append('1');
else line.append('0');
if (model->item(i)->checkState() == Qt::Checked) {
line.append('1');
}
else {
line.append('0');
}
}

stateFile->write(line.toLocal8Bit());
stateFile->close();
stateFile.write(line.toLocal8Bit());
stateFile.close();
}

void SWidgetList::load() {
if (!stateFile->open(QFile::ReadOnly | QFile::Text)) {
puts(stateFile->errorString().toLocal8Bit().data());
if (!stateFile.open(QFile::ReadOnly | QFile::Text)) {
qWarning() << stateFile.errorString();
return;
}

QStandardItemModel *model = (QStandardItemModel *)ui.listView->model();
QString line = QString(stateFile->readLine());
QString line = QString(stateFile.readLine());

for (int i = 0; (i < line.length()) && (i < model->rowCount()); i++) {
if (line.at(i) == '0') {
@@ -81,16 +88,24 @@ void SWidgetList::load() {
}
}

stateFile->close();
stateFile.close();
}

void SWidgetList::changeState(const QModelIndex &index) {
QStandardItemModel *model = (QStandardItemModel *)ui.listView->model();
if ((index.row() < 0) || (index.row() >= this->count())) return;
if (model->item(index.row())->checkState() == Qt::Checked) this->at(index.row())->activate();
else this->at(index.row())->activate(false);
if ((index.row() < 0) || (index.row() >= this->count())) {
return;
}
if (model->item(index.row())->checkState() == Qt::Checked) {
this->at(index.row())->activate();
}
else {
this->at(index.row())->activate(false);
}
}

void SWidgetList::setDesktop(QWidget *desktop) {
for(int i = 0; i < count(); i++) at(i)->setParent(desktop);
for(const auto& widget : (*this)) {
widget->setParent(desktop);
}
}
@@ -16,15 +16,15 @@
class SWidgetList : public QWidget, private QList <SWidget *> {
Q_OBJECT
public:
SWidgetList(QWidget *parent = 0);
void init(QWidget *desktop = 0, QString path = "");
SWidgetList(QWidget *parent = nullptr);
void init(QWidget *desktop = nullptr, const QString &path = "");
virtual ~SWidgetList();
void setVars(Satellite *sat, Location *loc, double *time);
inline void retranslateUi() { ui.retranslateUi(this); }
void setDesktop(QWidget *desktop);
private:
Ui::JsWidgetList ui;
QFile *stateFile;
QFile stateFile;
void load();
private slots:
void save();

0 comments on commit 464774e

Please sign in to comment.
You can’t perform that action at this time.