Skip to content
Permalink
Browse files

added widgets;

drawing area - draw colored boxes with different mouse buttons
  • Loading branch information...
ololuki committed May 11, 2017
1 parent 4e2a2ab commit de5430e0d860d54893f93cc4b7b97d82960e2a8e
@@ -15,8 +15,14 @@ TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS #emit warning if any deprecated feature is used

SOURCES += main.cpp\
mainwindow.cpp
mainwindow.cpp \
widgets/DrawingAreaView.cpp \
widgets/ColumnsDescriptionView.cpp \
widgets/RowsDescriptionView.cpp

HEADERS += mainwindow.h
HEADERS += mainwindow.h \
widgets/DrawingAreaView.h \
widgets/ColumnsDescriptionView.h \
widgets/RowsDescriptionView.h

FORMS += mainwindow.ui
@@ -13,7 +13,19 @@
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget"/>
<widget class="QWidget" name="centralWidget">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="1" alignment="Qt::AlignBottom">
<widget class="ColumnsDescriptionView" name="columnsDescription" native="true"/>
</item>
<item row="1" column="1">
<widget class="DrawingAreaView" name="drawingArea" native="true"/>
</item>
<item row="1" column="0" alignment="Qt::AlignRight">
<widget class="RowsDescriptionView" name="rowsDescription" native="true"/>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
@@ -65,6 +77,26 @@
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>DrawingAreaView</class>
<extends>QWidget</extends>
<header>widgets/DrawingAreaView.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>ColumnsDescriptionView</class>
<extends>QWidget</extends>
<header>widgets/ColumnsDescriptionView.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>RowsDescriptionView</class>
<extends>QWidget</extends>
<header>widgets/RowsDescriptionView.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
@@ -0,0 +1,6 @@
#include "ColumnsDescriptionView.h"

ColumnsDescriptionView::ColumnsDescriptionView(QWidget *parent) : QWidget(parent)
{
setMinimumSize(211, 211);
}
@@ -0,0 +1,17 @@
#ifndef COLUMNSDESCRIPTIONVIEW_H
#define COLUMNSDESCRIPTIONVIEW_H

#include <QWidget>

class ColumnsDescriptionView : public QWidget
{
Q_OBJECT
public:
explicit ColumnsDescriptionView(QWidget *parent = 0);

signals:

public slots:
};

#endif // COLUMNSDESCRIPTIONVIEW_H
@@ -0,0 +1,82 @@
#include "DrawingAreaView.h"
#include <QColor>
#include <QImage>
#include <QPoint>
#include <QWidget>
#include <QtWidgets>


DrawingAreaView::DrawingAreaView(QWidget *parent)
: QWidget(parent)
{
setAttribute(Qt::WA_StaticContents);
myPenWidth = 1;
myPenColor = Qt::black;

QSize size(211, 211);
resizeDrawingArea(size);
//setMinimumSize(211, 211); // for scroll area

lastPoint = QPoint(0, 0);
drawGrid();
}

void DrawingAreaView::mousePressEvent(QMouseEvent *event)
{
QPoint currentPoint = event->pos();

QPainter painter(&image);
painter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));

// square - mój prostokąt:
const int squareSize = 30;
int currentX = currentPoint.x() / squareSize * squareSize;
int currentY = currentPoint.y() / squareSize * squareSize;
QRect rectangle = QRect(currentX, currentY, squareSize, squareSize);

if (event->button() == Qt::LeftButton) {
painter.fillRect(rectangle, Qt::black);
}
if (event->button() == Qt::RightButton) {
painter.fillRect(rectangle, Qt::yellow);
}
if (event->button() == Qt::MiddleButton) {
painter.fillRect(rectangle, Qt::white);
}
painter.drawRect(rectangle);

update();
}

void DrawingAreaView::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QRect dirtyRect = event->rect();
painter.drawImage(dirtyRect, image, dirtyRect);
}

void DrawingAreaView::drawGrid()
{
QPainter p(&image);
p.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
for (int y = 0; y < 7; y++)
{
for (int x = 0; x < 7; x++)
{
int currentX = x * squareSize;
int currentY = y * squareSize;
QRect rectangle = QRect(currentX, currentY, squareSize, squareSize);
p.drawRect(rectangle);
}
}
}

void DrawingAreaView::resizeDrawingArea(const QSize &newSize)
{
setMinimumSize(newSize); // for scroll area, minimum size - you cannot shrink window size lower than this

// init image:
QImage newImage(newSize, QImage::Format_RGB32);
newImage.fill(qRgb(255, 255, 255));
image = newImage;
}
@@ -0,0 +1,27 @@
#ifndef DRAWINGAREAVIEW_H
#define DRAWINGAREAVIEW_H

#include <QWidget>

class DrawingAreaView : public QWidget
{
Q_OBJECT
public:
explicit DrawingAreaView(QWidget *parent = 0);
protected:
void mousePressEvent(QMouseEvent *event) override;
void paintEvent(QPaintEvent *event) override;

public slots:
private:
const int squareSize = 30;
void drawGrid();

void resizeDrawingArea(const QSize &newSize);
int myPenWidth;
QColor myPenColor;
QImage image;
QPoint lastPoint;
};

#endif // DRAWINGAREAVIEW_H
@@ -0,0 +1,6 @@
#include "RowsDescriptionView.h"

RowsDescriptionView::RowsDescriptionView(QWidget *parent) : QWidget(parent)
{
setMinimumSize(211, 211);
}
@@ -0,0 +1,17 @@
#ifndef ROWSDESCRIPTIONVIEW_H
#define ROWSDESCRIPTIONVIEW_H

#include <QWidget>

class RowsDescriptionView : public QWidget
{
Q_OBJECT
public:
explicit RowsDescriptionView(QWidget *parent = 0);

signals:

public slots:
};

#endif // ROWSDESCRIPTIONVIEW_H

0 comments on commit de5430e

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