Skip to content

Commit

Permalink
Merge pull request #48 from trikset/directCommands
Browse files Browse the repository at this point in the history
Direct commands
  • Loading branch information
yurii-litvinov committed Jul 18, 2014
2 parents 63c7b70 + e04f9af commit 348519e
Show file tree
Hide file tree
Showing 28 changed files with 482 additions and 332 deletions.
25 changes: 25 additions & 0 deletions global.pri
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# Build settings common to all projects in TrikRuntime.
# Provides:
# CONFIGURATION_SUFFIX variable that shall be consistently used in TARGET and LIBS variables in all projects.
# copyToDestdir function to copy arbitrary files and directories to DESTDIR
# uses function to automatically add a library to INCLUDEPATH and LIBS.

CROSS_COMPILE = $$(CROSS_COMPILE)

Expand All @@ -38,6 +40,11 @@ CONFIG(debug, debug | release) {

DESTDIR = $$PWD/bin/$$CONFIGURATION

PROJECT_BASENAME = $$basename(_PRO_FILE_)
PROJECT_NAME = $$section(PROJECT_BASENAME, ".", 0, 0)

TARGET = $$PROJECT_NAME$$CONFIGURATION_SUFFIX

equals(TEMPLATE, app) {
!macx {
QMAKE_LFLAGS += -Wl,-O1,-rpath,.
Expand All @@ -50,11 +57,16 @@ MOC_DIR = .build/$$CONFIGURATION/.moc
RCC_DIR = .build/$$CONFIGURATION/.rcc
UI_DIR = .build/$$CONFIGURATION/.ui

INCLUDEPATH += $$_PRO_FILE_PWD_ \
$$_PRO_FILE_PWD_/include/$$PROJECT_NAME \

unix {
target.path = $$[INSTALL_ROOT]/
INSTALLS += target
}

GLOBAL_PWD = $$PWD

# Useful function to copy additional files to destination,
# from http://stackoverflow.com/questions/3984104/qmake-how-to-copy-a-file-to-the-output
defineTest(copyToDestdir) {
Expand All @@ -77,3 +89,16 @@ defineTest(copyToDestdir) {

export(QMAKE_POST_LINK)
}

defineTest(uses) {
LIBS += -L$$DESTDIR
PROJECTS = $$1

for(PROJECT, PROJECTS) {
LIBS += -l$$PROJECT$$CONFIGURATION_SUFFIX
INCLUDEPATH += $$GLOBAL_PWD/$$PROJECT/include
}

export(LIBS)
export(INCLUDEPATH)
}
34 changes: 13 additions & 21 deletions trikCommunicator/trikCommunicator.pro
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,25 @@
# See the License for the specific language governing permissions and
# limitations under the License.

TRIKKERNEL_DIR = ../trikKernel/
TRIKCONTROL_DIR = ../trikControl/
TRIKSCRIPTRUNNER_DIR = ../trikScriptRunner/

TEMPLATE = lib

QT += network

DEFINES += TRIKCOMMUNICATOR_LIBRARY

include(../global.pri)

TARGET = trikCommunicator$$CONFIGURATION_SUFFIX

INCLUDEPATH = \
$$PWD \
$$PWD/include/trikCommunicator \
$$TRIKSCRIPTRUNNER_DIR/include \
$$TRIKCONTROL_DIR/include \
$$TRIKKERNEL_DIR/include \

LIBS += -L$$DESTDIR -ltrikScriptRunner$$CONFIGURATION_SUFFIX -ltrikKernel$$CONFIGURATION_SUFFIX

HEADERS += \
$$PWD/include/trikCommunicator/trikCommunicator.h \
$$PWD/src/connection.h \

SOURCES += \
$$PWD/src/trikCommunicator.cpp \
$$PWD/src/connection.cpp \

TEMPLATE = lib

QT += network

DEFINES += TRIKCOMMUNICATOR_LIBRARY

uses(trikScriptRunner trikControl trikKernel)

INCLUDEPATH += \
../trikScriptRunner/include/ \
../trikControl/include/ \
../trikKernel/include/ \
21 changes: 9 additions & 12 deletions trikControl/include/trikControl/sensor3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@
#pragma once

#include <QtCore/QObject>
#include <QtCore/QSocketNotifier>
#include <QtCore/QSharedPointer>
#include <QtCore/QScopedPointer>
#include <QtCore/QThread>
#include <QtCore/QVector>

#include "declSpec.h"

namespace trikControl {

class Sensor3dWorker;

/// Sensor that returns 3d vector.
class TRIKCONTROL_EXPORT Sensor3d : public QObject
{
Expand All @@ -35,20 +37,15 @@ class TRIKCONTROL_EXPORT Sensor3d : public QObject
/// @param deviceFile - device file for this sensor.
Sensor3d(int min, int max, QString const &deviceFile);

~Sensor3d();

public slots:
/// Returns current raw reading of a sensor in a form of vector with 3 coordinates.
QVector<int> const &read() const;

private slots:
/// Updates current reading when new value is ready.
void readFile();
QVector<int> read() const;

private:
QSharedPointer<QSocketNotifier> mSocketNotifier;
QVector<int> mReading;
int mDeviceFileDescriptor;
int mMax;
int mMin;
QScopedPointer<Sensor3dWorker> mSensor3dWorker;
QThread mWorkerThread;
};

}
1 change: 1 addition & 0 deletions trikControl/src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Display::Display(QThread &guiThread, const QString &startDirPath)
, mGuiWorker(new GuiWorker())
{
mGuiWorker->moveToThread(&guiThread);
QMetaObject::invokeMethod(mGuiWorker, "init");
}

Display::~Display()
Expand Down
90 changes: 49 additions & 41 deletions trikControl/src/guiWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,46 +30,54 @@
using namespace trikControl;

GuiWorker::GuiWorker()
: mFontMetrics(mImageWidget.font())
{

}

void GuiWorker::init()
{
mImageLabel.reset(new QLabel());
mImageWidget.reset(new GraphicsWidget());
mFontMetrics.reset(new QFontMetrics(mImageWidget->font()));

QHBoxLayout * const layout = new QHBoxLayout();
mImageLabel.setScaledContents(true);
layout->addWidget(&mImageLabel);
mImageWidget.setLayout(layout);
mImageWidget.setWindowState(Qt::WindowFullScreen);
mImageWidget.setWindowFlags(mImageWidget.windowFlags() | Qt::WindowStaysOnTopHint);
mImageLabel->setScaledContents(true);
layout->addWidget(mImageLabel.data());
mImageWidget->setLayout(layout);
mImageWidget->setWindowState(Qt::WindowFullScreen);
mImageWidget->setWindowFlags(mImageWidget->windowFlags() | Qt::WindowStaysOnTopHint);
resetBackground();
}

void GuiWorker::showImage(QString const &fileName)
{
if (!mImagesCache.contains(fileName)) {
QPixmap pixmap(fileName);
pixmap = pixmap.scaled(mImageWidget.size() - QSize(20, 20), Qt::KeepAspectRatio);
pixmap = pixmap.scaled(mImageWidget->size() - QSize(20, 20), Qt::KeepAspectRatio);
mImagesCache.insert(fileName, pixmap);
}

mImageLabel.setPixmap(mImagesCache.value(fileName));
mImageWidget.show();
mImageLabel->setPixmap(mImagesCache.value(fileName));
mImageWidget->show();
}

void GuiWorker::addLabel(QString const &text, int x, int y)
{
QLabel *label = findLabel(x, y);
label = label ? label : new QLabel(&mImageWidget);
label = label ? label : new QLabel(mImageWidget.data());
label->setText(text);
label->setStyleSheet(QString("color: %1").arg(mImageWidget.currentPenColor().name()));
label->setStyleSheet(QString("color: %1").arg(mImageWidget->currentPenColor().name()));

// There is no layout for the label, so its size cannot be set automatically. We set
// it with QFontMetrics.
label->setGeometry(x, y, mFontMetrics.width(text), mFontMetrics.height());
label->setGeometry(x, y, mFontMetrics->width(text), mFontMetrics->height());

label->show();
if (!mLabels.contains(x ^ y, label)) {
mLabels.insertMulti(x ^ y, label);
}

mImageWidget.show();
mImageWidget->show();
}

void GuiWorker::removeLabels()
Expand All @@ -89,7 +97,7 @@ void GuiWorker::deleteWorker()

void GuiWorker::setBackground(QString const &color)
{
QPalette palette = mImageWidget.palette();
QPalette palette = mImageWidget->palette();

if (color == tr("white")) {
palette.setColor(QPalette::Window, Qt::white);
Expand Down Expand Up @@ -129,41 +137,41 @@ void GuiWorker::setBackground(QString const &color)
palette.setColor(QPalette::Window, QColor(color));
}

mImageWidget.setPalette(palette);
mImageWidget.show();
mImageWidget->setPalette(palette);
mImageWidget->show();
}

void GuiWorker::resetBackground()
{
QPalette palette = mImageWidget.palette();
QPalette palette = mImageWidget->palette();
palette.setColor(QPalette::Window, Qt::lightGray);
mImageWidget.setPalette(palette);
mImageWidget->setPalette(palette);
}

void GuiWorker::setPainterColor(QString const &color)
{
mImageWidget.setPainterColor(color);
mImageWidget->setPainterColor(color);
}

void GuiWorker::setPainterWidth(int penWidth)
{
mImageWidget.setPainterWidth(penWidth);
mImageWidget->setPainterWidth(penWidth);
}

void GuiWorker::clear()
{
mImageWidget.deleteAllItems();
mImageWidget.setPainterColor("black");
mImageWidget.setPainterWidth(0);
mImageWidget.hide();
mImageWidget->deleteAllItems();
mImageWidget->setPainterColor("black");
mImageWidget->setPainterWidth(1);
mImageWidget->hide();
removeLabels();
mImageLabel.setPixmap(QPixmap());
mImageLabel->setPixmap(QPixmap());
resetBackground();
}

void GuiWorker::hide()
{
mImageWidget.hide();
mImageWidget->hide();
}

QLabel *GuiWorker::findLabel(int x, int y) const
Expand All @@ -179,35 +187,35 @@ QLabel *GuiWorker::findLabel(int x, int y) const

void GuiWorker::drawPoint(int x, int y)
{
mImageWidget.drawPoint(x, y);
mImageWidget.update();
mImageWidget.show();
mImageWidget->drawPoint(x, y);
mImageWidget->update();
mImageWidget->show();
}

void GuiWorker::drawLine(int x1, int y1, int x2, int y2)
{
mImageWidget.drawLine(x1, y1, x2, y2);
mImageWidget.update();
mImageWidget.show();
mImageWidget->drawLine(x1, y1, x2, y2);
mImageWidget->update();
mImageWidget->show();
}

void GuiWorker::drawRect(int x, int y, int width, int height)
{
mImageWidget.drawRect(x, y, width, height);
mImageWidget.update();
mImageWidget.show();
mImageWidget->drawRect(x, y, width, height);
mImageWidget->update();
mImageWidget->show();
}

void GuiWorker::drawEllipse(int x, int y, int width, int height)
{
mImageWidget.drawEllipse(x, y, width, height);
mImageWidget.update();
mImageWidget.show();
mImageWidget->drawEllipse(x, y, width, height);
mImageWidget->update();
mImageWidget->show();
}

void GuiWorker::drawArc(int x, int y, int width, int height, int startAngle, int spanAngle)
{
mImageWidget.drawArc(x, y, width, height, startAngle, spanAngle);
mImageWidget.update();
mImageWidget.show();
mImageWidget->drawArc(x, y, width, height, startAngle, spanAngle);
mImageWidget->update();
mImageWidget->show();
}
10 changes: 7 additions & 3 deletions trikControl/src/guiWorker.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <QtCore/qglobal.h>
#include <QtCore/QMultiHash>
#include <QtCore/QList>
#include <QtCore/QScopedPointer>
#include <QtGui/QPixmap>
#include <QtGui/QFontMetrics>

Expand Down Expand Up @@ -109,17 +110,20 @@ public slots:
/// @param spanAngle - end andle.
void drawArc(int x, int y, int width, int height, int startAngle, int spanAngle);

/// Initializes widget. Shall be called when widget is moved to correct thread. Not supposed to be called from .qts.
void init();

private:
void resetBackground();

/// Returns existing label with given coordinates or NULL if no such label exists.
QLabel *findLabel(int x, int y) const;

GraphicsWidget mImageWidget;
QLabel mImageLabel;
QScopedPointer<GraphicsWidget> mImageWidget;
QScopedPointer<QLabel> mImageLabel;
QHash<QString, QPixmap> mImagesCache;
QMultiHash<int, QLabel *> mLabels; // Has ownership.
QFontMetrics mFontMetrics;
QScopedPointer<QFontMetrics> mFontMetrics;
};

}
Loading

0 comments on commit 348519e

Please sign in to comment.