// 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 #include #define PI 3.1415 // 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(); // 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; czysc(); } // 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(); } // 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(); ClearImage(img); } void MyWindow::ApplyTempImage(){ memcpy(img->bits(), img_tmp->bits(), szer * wys * 4); } void MyWindow::UpdateTempImage(){ memcpy(img_tmp->bits(), img->bits(), szer * wys * 4); } QColor GetRandomColor(){ QColor res; res.setRed(rand() % 256); res.setGreen(rand() % 256); res.setBlue(rand() % 256); res.setAlpha(255); return res; } // Funkcja (slot) wywolywana po nacisnieciu przycisku myszy (w glownym oknie) void MyWindow::mousePressEvent(QMouseEvent *event) { UpdateTempImage(); // 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; startX = x; startY = y; active_img = img_tmp; draw_finished = false; mode = event->button() == Qt::LeftButton ? Circle : event->button() == Qt::RightButton ? Fill : Fill; if(mode == Fill){ FloodFill(img, x, y, QColor(0,0,0), GetRandomColor()); active_img = img; draw_finished = true; update(); } 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 || draw_finished){ ApplyTempImage(); active_img = img; draw_finished = true; update(); return; } if(mode == Circle){ DrawCircle(startX, startY, x, y); }else if(mode == Line){ DrawLine(startX, startY, x, y); } // Odswiezamy komponent update(); } void MyWindow::mouseReleaseEvent(QMouseEvent *event){ if(draw_finished) return; ApplyTempImage(); active_img = img; draw_finished = true; 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(); } QColor MyWindow::GetPixel(QImage* img, int x, int y){ QColor res; if(x >= szer || y >= wys || x < 0 || y < 0) return res; unsigned char* ptr = img->bits(); res.setBlue(ptr[szer*4*y + 4*x]); res.setGreen(ptr[szer*4*y + 4*x + 1]); res.setRed(ptr[szer*4*y + 4*x + 2]); res.setAlpha(ptr[szer*4*y + 4*x + 3]); return res; } void MyWindow::DrawLine(int x1, int y1, int x2, int y2, bool update_temp){ if(update_temp) UpdateTempImage(); unsigned char* ptr = img_tmp->bits(); 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) < 1.0f){ for(int x = x1; x <= x2; x++){ int x_form = x - x1; int y = a * x_form + y1; DrawPixel(img_tmp, 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_tmp, x, y, color); } } } void MyWindow::DrawCircle(int x1, int y1, int x2, int y2){ if(x2 >= szer || y2 >= wys || x2 < 0 || y2 < 0) return; UpdateTempImage(); unsigned char *ptr; ptr = img_tmp->bits(); float R = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2)); int q_last_x = R / sqrt(2) + 1; QColor color(255, 255, 255, 255); for(int x = 0; x <= q_last_x; x++){ float y = sqrt(pow(R, 2) - pow(x, 2)); for(int k = 0; k < 2; k++){ for(int i = 0; i < 2; i++){ for(int j = 0; j < 2; j++){ DrawPixel(img_tmp, x1 + x, y1 - y, color); y *= -1; } x *= -1; } int tmp = x; x = y; y = tmp; } } } void MyWindow::FloodFill(QImage* img, int x0, int y0, QColor zamieniany_color, QColor nowy_kolor) { std::stack> stack; zamieniany_color = GetPixel(img, x0,y0); stack.push(std::pair(x0, y0)); while (!stack.empty()) { int x = stack.top().first; int y = stack.top().second; stack.pop(); if (GetPixel(img, x, y) == zamieniany_color) { int w = x; int e = x; while (GetPixel(img, w, y) == zamieniany_color) w--; while (GetPixel(img, e, y) == zamieniany_color) e++; for (int i = w+1; i< e; i++) DrawPixel(img, i, y, nowy_kolor); for (int i = w+1; i< e; i++) { if (GetPixel(img,i,y+1) == zamieniany_color) stack.push(std::pair(i, y+1)); if (GetPixel(img,i,y-1) == zamieniany_color) stack.push(std::pair(i, y-1)); } } } return; } 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); } } }