// Dolaczamy plik naglowkowy naszej klasy MyWindow #include "mywindow.h" // Dolaczamy plik naglowkowy zawierajacy definicje GUI // Plik ten jest generowany automatycznie // z pliku XML "mywindow.ui" #include "ui_mywindow.h" #include #include #include #include #include #include // for float,double macros #include #define PI 3.1415 bool Intersects(Line a, Line b, Point& p){ return false; } int Shape::FindPoint(int x, int y, float radius){ Point src_point(x, y); float min_dist = FLT_MAX; int min_id = -1; for(int i = 0; i < (int)points.size(); i++){ float dist = (src_point - (Point)(points[i])).length(); if(dist < min_dist && dist < radius){ min_id = i; min_dist = dist; } } return min_id; } // Definicja konstruktora, wywolujemy najpierw // konstruktor klasy nadrzednej, nastepnie tworzymy // obiekt klasy Ui_MyWindow reprezentujacy GUI MyWindow::MyWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MyWindow) { // Wywolujemy funkcje tworzaca elementy GUI // Jej definicja znajduje sie w pliku "ui_mywindow.h" ui->setupUi(this); // Pobieramy wymiary i wspolrzedne lewego gornego naroznika ramki // i ustawiamy wartosci odpowiednich pol // Uwaga: ramke "rysujFrame" wykorzystujemy tylko do // wygodnego ustaiwenia tych wymiarow. Rysunek bedziemy wyswietlac // bezposrednio w glownym oknie aplikacji. szer = ui->rysujFrame->width(); wys = ui->rysujFrame->height(); poczX = ui->rysujFrame->x(); poczY = ui->rysujFrame->y(); segment_count = ui->segmentSlider->value(); QString str(("Ilosc segmentow: " + std::to_string(segment_count)).c_str()); ui->segmentLabel->setText(str); shapes.push_back(Shape()); // Tworzymy obiekt klasy QImage, o odpowiedniej szerokosci // i wysokosci. Ustawiamy format bitmapy na 32 bitowe RGB // (0xffRRGGBB). img = new QImage(szer,wys,QImage::Format_RGB32); img_tmp = new QImage(szer,wys,QImage::Format_RGB32); active_img = img; } // Definicja destruktora MyWindow::~MyWindow() { delete ui; } // Funkcja (slot) wywolywana po nacisnieciu przycisku "Wyjscie" (exitButton) // Uwaga: polaczenie tej funkcji z sygnalem "clicked" // emitowanym przez przycisk jest realizowane // za pomoca funkcji QMetaObject::connectSlotsByName(MyWindow) // znajdujacej sie w automatycznie generowanym pliku "ui_mywindow.h" // Nie musimy wiec sami wywolywac funkcji "connect" void MyWindow::on_exitButton_clicked() { // qApp to globalny wskaznik do obiektu reprezentujacego aplikacje // quit() to funkcja (slot) powodujaca zakonczenie aplikacji z kodem 0 (brak bledu) qApp->quit(); } void MyWindow::on_segmentSlider_valueChanged(int val) { segment_count = val; QString str(("Ilosc segmentow: " + std::to_string(segment_count)).c_str()); ui->segmentLabel->setText(str); DrawShapes(); update(); } // Funkcja "odmalowujaca" komponent void MyWindow::paintEvent(QPaintEvent*) { // Obiekt klasy QPainter pozwala nam rysowac na komponentach QPainter p(this); // Rysuje obrazek "img" w punkcie (poczX,poczY) // (tu bedzie lewy gorny naroznik) p.drawImage(poczX,poczY,*active_img); } // Funkcja (slot) wywolywana po nacisnieciu przycisku "Czysc" (cleanButton) void MyWindow::on_cleanButton_clicked() { // Funkcja czysci (zamalowuje na bialo) obszar rysowania // definicja znajduje sie ponizej czysc(); // Funkcja "update()" powoduje ponowne "namalowanie" calego komponentu // Wywoluje funkcje "paintEvent" update(); } // Funkcja powoduje wyczyszczenie (zamalowanie na bialo) // obszaru rysowania void MyWindow::czysc() { shapes.clear(); shapes.push_back(Shape()); DrawShapes(); } // Funkcja (slot) wywolywana po nacisnieciu przycisku myszy (w glownym oknie) void MyWindow::mousePressEvent(QMouseEvent *event) { // Pobieramy wspolrzedne punktu klikniecia int x = event->x(); int y = event->y(); // Sa to wspolrzedne wzgledem glownego okna, // Musimy odjac od nich wpolrzedne lewego gornego naroznika rysunku x -= poczX; y -= poczY; if(x >= szer || y >= wys || x < 0 || y < 0){ return; } int id = -1; int pt_id, sh_id; bool res; switch(event->button()){ case Qt::LeftButton: res = FindPoint(x, y, pt_id, sh_id, 20); if(!res){ mode = Add; shapes[shapes.size()-1].points.push_back(Point(x, y)); }else{ if(pt_id == 0 && shapes[shapes.size()-1].points.size() >= 3){ mode = Add; shapes.push_back(Shape()); }else{ mode = Move; tmp_point_id = pt_id; tmp_shape_id = sh_id; shapes[sh_id].points[pt_id].x = x; shapes[sh_id].points[pt_id].y = y; } } break; case Qt::RightButton: mode = Remove; res = FindPoint(x, y, pt_id, sh_id, 20); if(res){ // bezier_points.erase(bezier_points.begin() + id); } break; default: break; } DrawShapes(); update(); } void MyWindow::mouseMoveEvent(QMouseEvent *event) { // Pobieramy wspolrzedne punktu klikniecia int x = event->x(); int y = event->y(); // Sa to wspolrzedne wzgledem glownego okna, // Musimy odjac od nich wpolrzedne lewego gornego naroznika rysunku x -= poczX; y -= poczY; if(x >= szer || y >= wys || x < 0 || y < 0){ return; } switch(mode){ case Move: // bezier_points[tmp_point_id].x = x; // bezier_points[tmp_point_id].y = y; shapes[tmp_shape_id].points[tmp_point_id].x = x; shapes[tmp_shape_id].points[tmp_point_id].y = y; break; default: break; } DrawShapes(); // Odswiezamy komponent update(); } bool MyWindow::FindPoint(int x, int y, int& pt_id, int& shape_id, float radius){ for(int i = 0; i < (int)shapes.size(); i++){ int id; if((id = shapes[i].FindPoint(x, y, radius)) != -1){ shape_id = i; pt_id = id; return true; } } return false; } void MyWindow::DrawPixel(QImage* img, int x, int y, QColor color){ if(x >= szer || y >= wys || x < 0 || y < 0) return; unsigned char* ptr = img->bits(); ptr[szer*4*y + 4*x] = color.blue(); ptr[szer*4*y + 4*x + 1] = color.green(); ptr[szer*4*y + 4*x + 2] = color.red(); ptr[szer*4*y + 4*x + 3] = color.alpha(); } void MyWindow::DrawLine(int x1, int y1, int x2, int y2, QImage *img){ if(x1 > x2){ std::swap(x1, x2); std::swap(y1, y2); } float diff = x2 - x1; float a = diff != 0 ? (y2 - y1) / diff : FLT_MAX; QColor color(255, 255, 255, 255); if(abs(a) < 0.5f){ for(int x = x1; x <= x2; x++){ int x_form = x - x1; int y = a * x_form + y1; DrawPixel(img, x, y, color); } } else{ if(y1 > y2){ std::swap(x1, x2); std::swap(y1, y2); } float diff = x2 - x1; float a = diff != 0 ? (y2 - y1) / diff : FLT_MAX; for(int y = y1; y <= y2; y++){ int y_form = y - y1; int x = ((float)(y_form) / a) + x1; DrawPixel(img, x, y, color); } } } void MyWindow::DrawSquare(int x, int y, int size, QColor color){ size /= 2; for(int x1 = x - size; x1 < x + size; x1++){ for(int y1 = y - size; y1 < y + size; y1++){ if(x1 >= szer || y1 >= wys || x1 < 0 || y1 < 0) continue; DrawPixel(img, x1, y1, color); } } } void MyWindow::DrawShapes(){ ClearImage(img); for(int i = 0; i < (int)shapes.size(); i++){ for(int j = 0; j < (int)shapes[i].points.size() - 1; j++){ Point p1 = shapes[i].points[j]; Point p2 = shapes[i].points[j+1]; DrawLine(p1.x, p1.y, p2.x, p2.y, img); } if(i != (int)shapes.size() - 1 && (int)shapes[i].points.size() >= 2){ Point p1 = shapes[i].points[(int)shapes[i].points.size()-1]; Point p2 = shapes[i].points[0]; DrawLine(p1.x, p1.y, p2.x, p2.y, img); } std::vector lines; for(int j = 0; j < (int)shapes[i].points.size() - 1; j++){ Point p1 = shapes[i].points[j]; Point p2 = shapes[i].points[j+1]; lines.push_back(Line(p1, p2)); } for(int y = 0; y < wys; y++){ std::vector xs = GetIntersectionsWithY(y, lines); for(int l = 1; l < xs.size(); l+=2){ DrawLine(xs[l-1], y, xs[l], y, img); } } } } std::vector MyWindow::GetIntersectionsWithY(int y, std::vector lines){ std::vector res; for(int i = 0; i < lines.size(); i++){ Line l = lines[i]; if(l.A.x < l.B.x) std::swap(l.A, l.B); // if(l.A.x < l.B.x) // std::swap(l.A, l.B); if((l.A.y < y && l.B.y > y) || (l.A.y > y && l.B.y < y)){ float d1 = (y - l.A.y); float d2 = (l.B.y - l.A.y); float dx = (l.B.x - l.A.x); float ratio = d2/d1; int x = l.A.x + dx / ratio; res.push_back(x); } } std::sort(res.begin(), res.end(), std::greater()); return res; } void MyWindow::ClearImage(QImage *img){ unsigned char* empty_val = (unsigned char*)malloc(4); empty_val[0] = 0; empty_val[1] = 0; empty_val[2] = 0; empty_val[3] = 255; unsigned char* ptr = img->bits(); for(int i = 0; i < img->width(); i++){ for(int j = 0; j < img->height(); j++){ memcpy(ptr + 4 * (i + j * img->width()), empty_val, 4); } } }