Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <QtWidgets>
- #ifndef QT_NO_PRINTER
- #include <QPrintDialog>
- #endif
- #include<iostream>
- #include "imageviewer-qt5.h"
- ImageViewer::ImageViewer()
- {
- image=NULL;
- widthX = 0;
- resize(1600, 600);
- startLogging();
- generateMainGui();
- renewLogging();
- generateControlPanels();
- createActions();
- createMenus();
- resize(QGuiApplication::primaryScreen()->availableSize() * 0.85 );
- }
- void ImageViewer::getMittlereHelligkeit()
- {
- tempImage2 = image;
- tempImage = *tempImage2;
- for(int i = 0 ; i < tempImage2->height(); i++){
- for(int j = 0 ; j < tempImage2->width(); j++){
- tempLightness += qGray(tempImage2->pixel(j,i));
- }
- }
- renewLogging();
- tempLightness = tempLightness / (tempImage2->height() * tempImage2->width());
- QString str = "Mittlere helligkeit = " + QString::number(tempLightness);
- mittlereHelligkeit->setText(str);
- getVarianz();
- }
- void ImageViewer::getVarianz(){
- for(int i = 0 ; i < tempImage2->height(); i++){
- for(int j = 0 ; j < tempImage2->width(); j++){
- tempVarianz += pow((qGray(tempImage2->pixel(j,i)) - tempLightness),2);
- }
- }
- tempVarianz = tempVarianz / (tempImage2->height() * tempImage2->width());
- tempVarianz = sqrt(tempVarianz);
- QString str = "Varianz = " + QString::number(tempVarianz);
- varianz->setText(str);
- }
- void ImageViewer::erstelleHistogramm(){
- //fülle map mit 0 werten
- for(int h = 0 ;h<256;h++){
- histogramm[h] = 0;
- }
- //zähle die helligkeit aller pixel
- for(int i = 0 ; i < tempImage2->height(); i++){
- for(int j = 0 ; j < tempImage2->width(); j++){
- histogramm[qGray(tempImage2->pixel(j,i))] = histogramm.value(qGray(tempImage2->pixel(j,i)))+1;
- }
- }
- //whiteout mache das bild weiss
- loadFileExt("/home/stud/nwd-k92/Schreibtisch/CG2 2019/Aufgabe 1/white.jpg");
- //mahle die balken ins bild
- QRgb tempcolor = qRgb(0,0,1);
- int temppos = 0;
- int maxsize = 0;
- //ermitle grösten wert
- for(int t = 0;t<histogramm.size();t++){
- if(maxsize<histogramm.value(t)){
- maxsize = histogramm.value(t);
- }
- }
- //maxsize ist jetzt ein skalar um die balken in eine 200 pixel hohe bild rein zu bekommen
- maxsize = maxsize / image->height();
- //zeichne für jeden wert aus histogramm eine 2 pixel breiten und eine wert / maxsize grossen
- //balken
- for(int u = 0;u<histogramm.size();u++){
- logFile << u << " = " << histogramm.value(u) << std::endl;
- for(int g = 0; g<(int)(histogramm.value(u)/maxsize);g++){
- image->setPixel(temppos, g,tempcolor);
- image->setPixel(temppos+1, g,tempcolor);
- }
- temppos = temppos + 2;
- }
- updateImageDisplay();
- logFile << "histogram erstellt" << std::endl;
- renewLogging();
- }
- void ImageViewer::kontrastAnpassen(int h)
- {
- mult = 1 + (double)h/100;
- resetImage();
- for(int i = 0 ; i <image->height();i++){
- for(int j = 0; j<image->width();j++){
- tempPixel = image->pixel(j,i);
- y = qRed(tempPixel)*0.299+qGreen(tempPixel)*0.587+qBlue(tempPixel)*0.114;
- Cb = qRed(tempPixel)*(-0.169)+qGreen(tempPixel)*(-0.331)+qBlue(tempPixel)*0.5+128;
- Cr = qRed(tempPixel)*0.5+qGreen(tempPixel)*(-0.419)+qBlue(tempPixel)*(-0.081)+128;
- y = y * mult;
- Cb = Cb - 128;
- Cr = Cr - 128;
- tRed = (y + Cb * (-0.000926745) + Cr * 1.40169);
- tGreen = (y + Cb * (-0.343695) + Cr * (-0.714169));
- tBlue = (y + Cb * 1.77216 + Cr * 0.000990221);
- if( tRed>255 ){tRed = 255;}
- if( tRed<0 ){tRed = 0;}
- if( tGreen>255 ){tGreen = 255;}
- if( tGreen<0 ){tGreen = 0;}
- if( tBlue>255 ){tBlue = 255;}
- if( tBlue<0 ){tBlue = 0;}
- image->setPixel(j,i,qRgb(tRed,tGreen,tBlue));
- }
- }
- updateImageDisplay();
- }
- void ImageViewer::helligkeitAnpassen(int h)
- {
- resetImage();
- for(int i = 0 ; i <image->height();i++){
- for(int j = 0; j<image->width();j++){
- tempPixel = image->pixel(j,i);
- y = qRed(tempPixel)*0.299+qGreen(tempPixel)*0.587+qBlue(tempPixel)*0.114;
- Cb = qRed(tempPixel)*(-0.169)+qGreen(tempPixel)*(-0.331)+qBlue(tempPixel)*0.5+128;
- Cr = qRed(tempPixel)*0.5+qGreen(tempPixel)*(-0.419)+qBlue(tempPixel)*(-0.081)+128;
- y = y + h;
- Cb = Cb - 128;
- Cr = Cr - 128;
- tRed = (y + Cb * (-0.000926745) + Cr * 1.40169);
- tGreen = (y + Cb * (-0.343695) + Cr * (-0.714169));
- tBlue = (y + Cb * 1.77216 + Cr * 0.000990221);
- if( tRed>255 ){tRed = 255;}
- if( tRed<0 ){tRed = 0;}
- if( tGreen>255 ){tGreen = 255;}
- if( tGreen<0 ){tGreen = 0;}
- if( tBlue>255 ){tBlue = 255;}
- if( tBlue<0 ){tBlue = 0;}
- image->setPixel(j,i,qRgb(tRed,tGreen,tBlue));
- }
- }
- updateImageDisplay();
- }
- void ImageViewer::applyExampleAlgorithm()
- {
- if(image!=NULL)
- {
- resetImage();
- for(int i=0;i<std::min(image->width(),image->height());i++)
- {
- // macht die Farbe schwarz, bitte recherchieren wie eine andere Farbe gesetzt wird ...
- for(int j=0;j<widthX;j++){
- value = qRgb(255, 0, 0);
- if(i+j<image->width()){
- image->setPixel(i+j, i, value);
- }
- }
- }
- }
- updateImageDisplay();
- logFile << "example algorithm applied " << std::endl;
- renewLogging();
- }
- void ImageViewer::doSmth()
- {
- logFile << "done something " << std::endl;
- renewLogging();
- }
- void ImageViewer::resetImage()
- {
- if(image!=NULL)
- {
- delete image;
- image=NULL;
- }
- image = new QImage(imageOld);
- updateImageDisplay();
- }
- void ImageViewer::inkWidthX(int i)
- {
- widthX = i;
- }
- void ImageViewer::AktualesiereKontrast(int i)
- {
- tempstr = "kontrast = " + QString::number(i);
- kontrastSlider->setText(tempstr);
- }
- void ImageViewer::AktualesiereHell(int i)
- {
- tempstr = "helligkeit = " + QString::number(i);
- helligkeitSlider->setText(tempstr);
- }
- void ImageViewer::autoKontrast()
- {
- mult = (double)spinbox2->value()/100;
- aLow = 255 * mult;
- aHigh = 255 - aLow;
- resetImage();
- helligkeitAnpassen(aLow*(-1));
- for(int i = 0 ; i <image->height();i++){
- for(int j = 0; j<image->width();j++){
- tempPixel = image->pixel(j,i);
- y = qRed(tempPixel)*0.299+qGreen(tempPixel)*0.587+qBlue(tempPixel)*0.114;
- Cb = qRed(tempPixel)*(-0.169)+qGreen(tempPixel)*(-0.331)+qBlue(tempPixel)*0.5+128;
- Cr = qRed(tempPixel)*0.5+qGreen(tempPixel)*(-0.419)+qBlue(tempPixel)*(-0.081)+128;
- y = y * (1+mult);
- Cb = Cb - 128;
- Cr = Cr - 128;
- tRed = (y + Cb * (-0.000926745) + Cr * 1.40169);
- tGreen = (y + Cb * (-0.343695) + Cr * (-0.714169));
- tBlue = (y + Cb * 1.77216 + Cr * 0.000990221);
- if( tRed>255 ){tRed = 255;}
- if( tRed<0 ){tRed = 0;}
- if( tGreen>255 ){tGreen = 255;}
- if( tGreen<0 ){tGreen = 0;}
- if( tBlue>255 ){tBlue = 255;}
- if( tBlue<0 ){tBlue = 0;}
- image->setPixel(j,i,qRgb(tRed,tGreen,tBlue));
- }
- }
- updateImageDisplay();
- }
- void ImageViewer::restoreImage()
- {
- loadFile(origImage);
- }
- void ImageViewer::updateColomn(int i)
- {
- table->setColumnCount(i);
- for(int i = 0 ; i < table->width();i++){
- for(int j = 0;j < table->height();j++){
- table->setItem(i,j,new QTableWidgetItem("1"));
- }
- }
- }
- void ImageViewer::updateRow(int i)
- {
- table->setRowCount(i);
- for(int i = 0 ; i < table->width();i++){
- for(int j = 0;j < table->height();j++){
- table->setItem(i,j,new QTableWidgetItem("1"));
- }
- }
- }
- void ImageViewer::gausFilterAnwenden()
- {
- ho.clear();
- sumFaktor = 0;
- sigma = spinbox5->value();
- center = (int)(3.0 * sigma);
- sigma2 = sigma * sigma ;
- for(int i = 0; i<(2*center+1);i++){
- double r = center - i;
- ho.push_back(exp(-0.5 * (r*r) / sigma2));
- sumFaktor = sumFaktor + ho[i];
- }
- hSize = ho.size();
- filteredImage = image->copy(QRect(0,0,image->width(),image->height()));
- PfilteredImage = &filteredImage;
- // wende filter an
- for(int x = 0; x<image->width();x++){
- for(int y = 0;y<image->height();y++){
- tRed = 0;
- tGreen = 0;
- tBlue = 0;
- for(int w = (-1)*(int)(hSize/2) ;w<=(int)(hSize/2);w++){
- tempMaxY = y+w;
- faktor = ho[w+hSize/2];
- if(tempMaxY>=image->height()){tempMaxY=image->height()-1;}
- if(tempMaxY<0){tempMaxY=0;}
- tRed = tRed + (int)(qRed(PfilteredImage->pixel(x,tempMaxY)) * faktor);
- tGreen = tGreen + (int)(qGreen(PfilteredImage->pixel(x,tempMaxY)) * faktor);
- tBlue = tBlue + (int)(qBlue(PfilteredImage->pixel(x,tempMaxY)) * faktor);
- }
- tRed = tRed /sumFaktor;
- tGreen = tGreen /sumFaktor;
- tBlue = tBlue /sumFaktor;
- if( tRed>255 ){tRed = 255;}
- if( tRed<0 ){tRed = 0;}
- if( tGreen>255 ){tGreen = 255;}
- if( tGreen<0 ){tGreen = 0;}
- if( tBlue>255 ){tBlue = 255;}
- if( tBlue<0 ){tBlue = 0;}
- PfilteredImage->setPixel(x,y,qRgb(tRed,tGreen,tBlue));
- }
- }
- for(int x = 0; x<image->width();x++){
- for(int y = 0;y<image->height();y++){
- tRed = 0;
- tGreen = 0;
- tBlue = 0;
- for(int w = (-1)*(int)(hSize/2) ;w<=(int)(hSize/2);w++){
- tempMaxX = x+w;
- faktor = ho[w+hSize/2];
- if(tempMaxX>=image->width()){tempMaxX=image->width()-1;}
- if(tempMaxX<0){tempMaxX=0;}
- tRed = tRed + (int)(qRed(PfilteredImage->pixel(tempMaxX,y)) * faktor);
- tGreen = tGreen + (int)(qGreen(PfilteredImage->pixel(tempMaxX,y)) * faktor);
- tBlue = tBlue + (int)(qBlue(PfilteredImage->pixel(tempMaxX,y)) * faktor);
- }
- tRed = tRed /sumFaktor;
- tGreen = tGreen /sumFaktor;
- tBlue = tBlue /sumFaktor;
- if( tRed>255 ){tRed = 255;}
- if( tRed<0 ){tRed = 0;}
- if( tGreen>255 ){tGreen = 255;}
- if( tGreen<0 ){tGreen = 0;}
- if( tBlue>255 ){tBlue = 255;}
- if( tBlue<0 ){tBlue = 0;}
- image->setPixel(x,y,qRgb(tRed,tGreen,tBlue));
- }
- }
- updateImageDisplay();
- }
- void ImageViewer::kantDetSobel(){
- spinbox3 -> setValue(3);
- spinbox4 -> setValue(3);
- table->setItem(0,0,new QTableWidgetItem("-1"));
- table->setItem(0,1,new QTableWidgetItem("0"));
- table->setItem(0,2,new QTableWidgetItem("1"));
- table->setItem(1,0,new QTableWidgetItem("-1"));
- table->setItem(1,1,new QTableWidgetItem("0"));
- table->setItem(1,2,new QTableWidgetItem("1"));
- table->setItem(2,0,new QTableWidgetItem("-1"));
- table->setItem(2,1,new QTableWidgetItem("0"));
- table->setItem(2,2,new QTableWidgetItem("1"));
- filterAnwenden();
- sobelXImage = image->copy(QRect(0,0,image->width(),image->height()));
- resetImage();
- table->setItem(0,0,new QTableWidgetItem("-1"));
- table->setItem(0,1,new QTableWidgetItem("-1"));
- table->setItem(0,2,new QTableWidgetItem("-1"));
- table->setItem(1,0,new QTableWidgetItem("0"));
- table->setItem(1,1,new QTableWidgetItem("0"));
- table->setItem(1,2,new QTableWidgetItem("0"));
- table->setItem(2,0,new QTableWidgetItem("1"));
- table->setItem(2,1,new QTableWidgetItem("1"));
- table->setItem(2,2,new QTableWidgetItem("1"));
- filterAnwenden();
- sobelYImage = image->copy(QRect(0,0,image->width(),image->height()));
- resetImage();
- for(int i = 0; i < image->height();i++){
- for(int j = 0; j < image->width();j++){
- tempXSobelPixel = sobelXImage.pixel(j,i);
- tempYSobelPixel = sobelYImage.pixel(j,i);
- tRed = ((int)(qRed(tempXSobelPixel))+(int)(qRed(tempYSobelPixel)));
- tGreen = ((int)(qGreen(tempXSobelPixel))+(int)(qGreen(tempYSobelPixel)));
- tBlue = ((int)(qBlue(tempXSobelPixel))+(int)(qBlue(tempYSobelPixel)));
- image->setPixel(j,i,qRgb(tRed,tGreen,tBlue));
- }
- }
- updateImageDisplay();
- /* 1). wende filterAnwenden() mit sobel matrix in x richtung an
- * 2). speichere entstandenes bild in copyA
- * 3). wende filterAnwenden() mit sobel matrix in y richtung an
- * 4). speichere entstandenes bild in copyB
- * 5). summiere die pixel von copyA und copyB zu neuem Image
- */
- }
- void ImageViewer::filterAnwenden()
- {
- filteredImage = image->copy(QRect(0,0,image->width(),image->height()));
- PfilteredImage = &filteredImage;
- // wende filter an
- for(int x = 0; x<image->width();x++){
- for(int y = 0;y<image->height();y++){
- tRed = 0;
- tGreen = 0;
- tBlue = 0;
- for(int w = (-1)*(int)(spinbox3->value()/2) ;w<=(int)(spinbox3->value()/2);w++){
- for(int h = (-1)*(int)(spinbox4->value()/2) ;h<=(int)(spinbox4->value()/2);h++){
- tempMaxX = x+w;
- tempMaxY = y+h;
- pTableItem = (table->item(h+(int)(spinbox4->value()/2),w+(int)(spinbox3->value()/2)));
- if(tempMaxX>=image->width()){tempMaxX=image->width()-1;}
- if(tempMaxY>=image->height()){tempMaxY=image->height()-1;}
- if(tempMaxX<0){tempMaxX=0;}
- if(tempMaxY<0){tempMaxY=0;}
- tRed = tRed + (int)(qRed(PfilteredImage->pixel(tempMaxX,tempMaxY)) * (*pTableItem).text().toDouble());
- tGreen = tGreen + (int)(qGreen(PfilteredImage->pixel(tempMaxX,tempMaxY)) * (*pTableItem).text().toDouble());
- tBlue = tBlue + (int)(qBlue(PfilteredImage->pixel(tempMaxX,tempMaxY)) * (*pTableItem).text().toDouble());
- }
- }
- tRed = tRed /(spinbox3->value()*spinbox4->value());
- tGreen = tGreen /(spinbox3->value()*spinbox4->value());
- tBlue = tBlue /(spinbox3->value()*spinbox4->value());
- if( tRed>255 ){tRed = 255;}
- if( tRed<0 ){tRed = 0;}
- if( tGreen>255 ){tGreen = 255;}
- if( tGreen<0 ){tGreen = 0;}
- if( tBlue>255 ){tBlue = 255;}
- if( tBlue<0 ){tBlue = 0;}
- image->setPixel(x,y,qRgb(tRed,tGreen,tBlue));
- }
- }
- // image = PfilteredImage;
- updateImageDisplay();
- }
- /****************************************************************************************
- *
- * mit dieser Methode können sie sich pro Aufgabe ein Tab anlegen, in der die Ein-
- * stellungen per Slider, Button, Checkbox etc. gemacht werden und die zu implemen-
- * tierenden Algorithmen gestatet werden.
- *
- *****************************************************************************************/
- void ImageViewer::generateControlPanels()
- {
- // first tab-----------------------------------------------------
- m_option_panel1 = new QWidget();
- m_option_layout1 = new QVBoxLayout();
- m_option_panel1->setLayout(m_option_layout1);
- //tabelle vorher mit 1-en füllen
- button1 = new QPushButton();
- button1->setText("Apply algorithm");
- button2 = new QPushButton();
- button2->setText("do something else");
- button3 = new QPushButton();
- button3->setText("reset Image");
- spinbox1 = new QSpinBox(tabWidget);
- spinbox1 -> setRange(0,20);
- spinbox1 -> setSingleStep(1);
- spinbox1 -> setValue(0);
- QObject::connect(button1, SIGNAL (clicked()), this, SLOT (applyExampleAlgorithm()));
- QObject::connect(button2, SIGNAL (clicked()), this, SLOT (doSmth()));
- QObject::connect(button3, SIGNAL (clicked()), this, SLOT (resetImage()));
- QObject::connect(spinbox1, SIGNAL (valueChanged(int)), this, SLOT (inkWidthX(int)));
- m_option_layout1->addWidget(new QLabel("string thikness."));
- m_option_layout1->addWidget(spinbox1);
- m_option_layout1->addWidget(button1);
- m_option_layout1->addWidget(button2);
- m_option_layout1->addWidget(button3);
- tabWidget->addTab(m_option_panel1,"first tab");
- // second tab--------------------------------------------------------------------
- m_option_panel2 = new QWidget();
- m_option_layout2 = new QVBoxLayout();
- m_option_panel2->setLayout(m_option_layout2);
- mittlereHelligkeit = new QLabel("Mittlere helligkeit = ");
- varianz = new QLabel("Varianz = ");
- button6 = new QPushButton();
- button6->setText("ermittle Mittlere Hell. und Varianz");
- button4 = new QPushButton();
- button4->setText("erstelle histogramm");
- slider1 = new QSlider();
- slider1->setSingleStep(1);
- slider1->setMaximum(25);
- slider1->setMinimum(-25);
- slider1->setOrientation(Qt::Horizontal);
- helligkeitSlider = new QLabel("helligkeit = 1");
- slider2 = new QSlider();
- slider2->setSingleStep(1);
- slider2->setMaximum(25);
- slider2->setMinimum(-25);
- slider2->setOrientation(Qt::Horizontal);
- kontrastSlider = new QLabel("kontrast = 1");
- spinbox2 = new QSpinBox(tabWidget);
- spinbox2 -> setRange(0,20);
- spinbox2 -> setSingleStep(1);
- spinbox2 -> setValue(0);
- button5 = new QPushButton();
- button5->setText("automatische kontrastanpassung");
- button7 = new QPushButton();
- button7->setText("kehre zu bild zurück");
- QObject::connect(button6, SIGNAL (clicked()), this, SLOT (getMittlereHelligkeit()));
- QObject::connect(button5, SIGNAL (clicked()), this, SLOT (autoKontrast()));
- QObject::connect(slider1, SIGNAL (sliderMoved(int)), this, SLOT (AktualesiereHell(int)));
- QObject::connect(slider2, SIGNAL (sliderMoved(int)), this, SLOT (AktualesiereKontrast(int)));
- QObject::connect(slider1, SIGNAL (sliderMoved(int)), this, SLOT (helligkeitAnpassen(int)));
- QObject::connect(slider2, SIGNAL (sliderMoved(int)), this, SLOT (kontrastAnpassen(int)));
- QObject::connect(button4, SIGNAL (clicked()), this, SLOT (erstelleHistogramm()));
- QObject::connect(button7, SIGNAL (clicked()), this, SLOT (restoreImage()));
- m_option_layout2->addWidget(mittlereHelligkeit);
- m_option_layout2->addWidget(varianz);
- m_option_layout2->addWidget(button6);
- m_option_layout2->addWidget(button4);
- m_option_layout2->addWidget(helligkeitSlider);
- m_option_layout2->addWidget(slider1);
- m_option_layout2->addWidget(kontrastSlider);
- m_option_layout2->addWidget(slider2);
- m_option_layout2->addWidget(spinbox2);
- m_option_layout2->addWidget(button5);
- m_option_layout2->addWidget(button7);
- tabWidget->addTab(m_option_panel2,"second tab");
- //third tab-------------------------------------------------------
- m_option_panel3 = new QWidget();
- m_option_layout3 = new QVBoxLayout();
- m_option_panel3->setLayout(m_option_layout3);
- filterSize = new QLabel("Filter Grösse X oben, Y Unten");
- spinbox3 = new QSpinBox(tabWidget);
- spinbox3 -> setRange(1,15);
- spinbox3 -> setSingleStep(2);
- spinbox3 -> setValue(5);
- spinbox4 = new QSpinBox(tabWidget);
- spinbox4 -> setRange(1,15);
- spinbox4 -> setSingleStep(2);
- spinbox4 -> setValue(5);
- button8 = new QPushButton();
- button8->setText("Filter anwenden");
- table = new QTableWidget();
- table->setColumnCount(spinbox3->value());
- table->setRowCount(spinbox4->value());
- for(int i = 0 ; i < table->width();i++){
- for(int j = 0;j < table->height();j++){
- table->setItem(i,j,new QTableWidgetItem("1"));
- }
- }
- gausFilter = new QLabel("2D-Gausfilter");
- spinbox5 = new QSpinBox(tabWidget);
- spinbox5 -> setRange(0,15);
- spinbox5 -> setSingleStep(2);
- spinbox5 -> setValue(3);
- button9 = new QPushButton();
- button9->setText("wende Gaus filter an");
- button10 = new QPushButton();
- button10->setText("Reset Image");
- QObject::connect(button8, SIGNAL (clicked()), this, SLOT (filterAnwenden()));
- QObject::connect(spinbox3, SIGNAL (valueChanged(int)), this, SLOT (updateColomn(int)));
- QObject::connect(spinbox4, SIGNAL (valueChanged(int)), this, SLOT (updateRow(int)));
- QObject::connect(button9, SIGNAL (clicked()), this, SLOT (gausFilterAnwenden()));
- QObject::connect(button10, SIGNAL (clicked()), this, SLOT (resetImage()));
- m_option_layout3->addWidget(filterSize);
- m_option_layout3->addWidget(spinbox3);
- m_option_layout3->addWidget(spinbox4);
- m_option_layout3->addWidget(button8);
- m_option_layout3->addWidget(table);
- m_option_layout3->addWidget(gausFilter);
- m_option_layout3->addWidget(spinbox5);
- m_option_layout3->addWidget(button9);
- m_option_layout3->addWidget(button10);
- tabWidget->addTab(m_option_panel3,"third tab");
- //forth tab-------------------------------------------------------
- m_option_panel4 = new QWidget();
- m_option_layout4 = new QVBoxLayout();
- m_option_panel4->setLayout(m_option_layout4);
- button11 = new QPushButton();
- button11->setText("Use Sobel Kantenoperation");
- QObject::connect(button11, SIGNAL (clicked()), this, SLOT (kantDetSobel()));
- m_option_layout4->addWidget(button11);
- tabWidget->addTab(m_option_panel4,"forth tab");
- tabWidget->show();
- // Hinweis: Es bietet sich an pro Aufgabe jeweils einen solchen Tab zu erstellen
- }
- /****************************************************************************************
- *
- * ab hier kommen technische Details, die nicht notwenig für das Verständnis und die
- * Bearbeitung sind.
- *
- *
- *****************************************************************************************/
- void ImageViewer::startLogging()
- {
- //LogFile
- logFile.open("log.txt", std::ios::out);
- logFile << "Logging: \n" << std::endl;
- }
- void ImageViewer::renewLogging()
- {
- QFile file("log.txt"); // Create a file handle for the file named
- QString line;
- file.open(QIODevice::ReadOnly); // Open the file
- QTextStream stream( &file ); // Set the stream to read from myFile
- logBrowser->clear();
- while(!stream.atEnd()){
- line = stream.readLine(); // this reads a line (QString) from the file
- logBrowser->append(line);
- }
- }
- void ImageViewer::resizeEvent(QResizeEvent * event)
- {
- QMainWindow::resizeEvent(event);
- centralwidget->setMinimumWidth(width());
- centralwidget->setMinimumHeight(height());
- centralwidget->setMaximumWidth(width());
- centralwidget->setMaximumHeight(height());
- logBrowser->setMinimumWidth(width()-40);
- logBrowser->setMaximumWidth(width()-40);
- }
- void ImageViewer::updateImageDisplay()
- {
- imageLabel->setPixmap(QPixmap::fromImage(*image));
- }
- void ImageViewer::generateMainGui()
- {
- /* Tab widget */
- tabWidget = new QTabWidget(this);
- tabWidget->setObjectName(QStringLiteral("tabWidget"));
- /* Center widget */
- centralwidget = new QWidget(this);
- centralwidget->setObjectName(QStringLiteral("centralwidget"));
- centralwidget->setFixedSize(1000,300);
- //setCentralWidget(centralwidget);
- imageLabel = new QLabel;
- imageLabel->setBackgroundRole(QPalette::Base);
- imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
- imageLabel->setScaledContents(true);
- /* Center widget */
- scrollArea = new QScrollArea;
- scrollArea->setBackgroundRole(QPalette::Dark);
- scrollArea->setWidget(imageLabel);
- setCentralWidget(scrollArea);
- /* HBox layout */
- QGridLayout* gLayout = new QGridLayout(centralwidget);
- gLayout->setObjectName(QStringLiteral("hboxLayout"));
- gLayout->addWidget(new QLabel(),1,1);
- gLayout->setVerticalSpacing(50);
- gLayout->addWidget(tabWidget,2,1);
- gLayout->addWidget(scrollArea,2,2);
- logBrowser= new QTextEdit(this);
- logBrowser->setMinimumHeight(100);
- logBrowser->setMaximumHeight(200);
- logBrowser->setMinimumWidth(width());
- logBrowser->setMaximumWidth(width());
- gLayout->addWidget(logBrowser,3,1,1,2);
- gLayout->setVerticalSpacing(50);
- }
- bool ImageViewer::loadFile(const QString &fileName)
- {
- if(image!=NULL)
- {
- delete image;
- image=NULL;
- }
- image = new QImage(fileName);
- imageOld = fileName;
- if (image->isNull()) {
- QMessageBox::information(this, QGuiApplication::applicationDisplayName(),
- tr("Cannot load %1.").arg(QDir::toNativeSeparators(fileName)));
- setWindowFilePath(QString());
- imageLabel->setPixmap(QPixmap());
- imageLabel->adjustSize();
- return false;
- }
- scaleFactor = 1.0;
- updateImageDisplay();
- printAct->setEnabled(true);
- fitToWindowAct->setEnabled(true);
- updateActions();
- if (!fitToWindowAct->isChecked())
- imageLabel->adjustSize();
- setWindowFilePath(fileName);
- logFile << "geladen: " << fileName.toStdString().c_str() << std::endl;
- renewLogging();
- return true;
- }
- bool ImageViewer::loadFileExt(const QString &fileName)
- {
- origImage = imageOld;
- loadFile(fileName);
- return true;
- }
- void ImageViewer::open()
- {
- QStringList mimeTypeFilters;
- foreach (const QByteArray &mimeTypeName, QImageReader::supportedMimeTypes())
- mimeTypeFilters.append(mimeTypeName);
- mimeTypeFilters.sort();
- const QStringList picturesLocations = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation);
- QFileDialog dialog(this, tr("Open File"),
- picturesLocations.isEmpty() ? QDir::currentPath() : picturesLocations.first());
- dialog.setAcceptMode(QFileDialog::AcceptOpen);
- dialog.setMimeTypeFilters(mimeTypeFilters);
- dialog.selectMimeTypeFilter("image/jpeg");
- while (dialog.exec() == QDialog::Accepted && !loadFile(dialog.selectedFiles().first())) {}
- }
- void ImageViewer::print()
- {
- Q_ASSERT(imageLabel->pixmap());
- #if !defined(QT_NO_PRINTER) && !defined(QT_NO_PRINTDIALOG)
- QPrintDialog dialog(&printer, this);
- if (dialog.exec()) {
- QPainter painter(&printer);
- QRect rect = painter.viewport();
- QSize size = imageLabel->pixmap()->size();
- size.scale(rect.size(), Qt::KeepAspectRatio);
- painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
- painter.setWindow(imageLabel->pixmap()->rect());
- painter.drawPixmap(0, 0, *imageLabel->pixmap());
- }
- #endif
- }
- void ImageViewer::zoomIn()
- {
- scaleImage(1.25);
- }
- void ImageViewer::zoomOut()
- {
- scaleImage(0.8);
- }
- void ImageViewer::normalSize()
- {
- imageLabel->adjustSize();
- scaleFactor = 1.0;
- }
- void ImageViewer::fitToWindow()
- {
- bool fitToWindow = fitToWindowAct->isChecked();
- scrollArea->setWidgetResizable(fitToWindow);
- if (!fitToWindow) {
- normalSize();
- }
- updateActions();
- }
- void ImageViewer::about()
- {
- QMessageBox::about(this, tr("About Image Viewer"),
- tr("<p>The <b>Image Viewer</b> example shows how to combine QLabel "
- "and QScrollArea to display an image. QLabel is typically used "
- "for displaying a text, but it can also display an image. "
- "QScrollArea provides a scrolling view around another widget. "
- "If the child widget exceeds the size of the frame, QScrollArea "
- "automatically provides scroll bars. </p><p>The example "
- "demonstrates how QLabel's ability to scale its contents "
- "(QLabel::scaledContents), and QScrollArea's ability to "
- "automatically resize its contents "
- "(QScrollArea::widgetResizable), can be used to implement "
- "zooming and scaling features. </p><p>In addition the example "
- "shows how to use QPainter to print an image.</p>"));
- }
- void ImageViewer::createActions()
- {
- openAct = new QAction(tr("&Open..."), this);
- openAct->setShortcut(tr("Ctrl+O"));
- connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
- printAct = new QAction(tr("&Print..."), this);
- printAct->setShortcut(tr("Ctrl+P"));
- printAct->setEnabled(false);
- connect(printAct, SIGNAL(triggered()), this, SLOT(print()));
- exitAct = new QAction(tr("E&xit"), this);
- exitAct->setShortcut(tr("Ctrl+Q"));
- connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
- zoomInAct = new QAction(tr("Zoom &In (25%)"), this);
- zoomInAct->setShortcut(tr("Ctrl++"));
- zoomInAct->setEnabled(false);
- connect(zoomInAct, SIGNAL(triggered()), this, SLOT(zoomIn()));
- zoomOutAct = new QAction(tr("Zoom &Out (25%)"), this);
- zoomOutAct->setShortcut(tr("Ctrl+-"));
- zoomOutAct->setEnabled(false);
- connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(zoomOut()));
- normalSizeAct = new QAction(tr("&Normal Size"), this);
- normalSizeAct->setShortcut(tr("Ctrl+S"));
- normalSizeAct->setEnabled(false);
- connect(normalSizeAct, SIGNAL(triggered()), this, SLOT(normalSize()));
- fitToWindowAct = new QAction(tr("&Fit to Window"), this);
- fitToWindowAct->setEnabled(false);
- fitToWindowAct->setCheckable(true);
- fitToWindowAct->setShortcut(tr("Ctrl+F"));
- connect(fitToWindowAct, SIGNAL(triggered()), this, SLOT(fitToWindow()));
- aboutAct = new QAction(tr("&About"), this);
- connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
- aboutQtAct = new QAction(tr("About &Qt"), this);
- connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
- }
- void ImageViewer::createMenus()
- {
- fileMenu = new QMenu(tr("&File"), this);
- fileMenu->addAction(openAct);
- fileMenu->addAction(printAct);
- fileMenu->addSeparator();
- fileMenu->addAction(exitAct);
- viewMenu = new QMenu(tr("&View"), this);
- viewMenu->addAction(zoomInAct);
- viewMenu->addAction(zoomOutAct);
- viewMenu->addAction(normalSizeAct);
- viewMenu->addSeparator();
- viewMenu->addAction(fitToWindowAct);
- helpMenu = new QMenu(tr("&Help"), this);
- helpMenu->addAction(aboutAct);
- helpMenu->addAction(aboutQtAct);
- menuBar()->addMenu(fileMenu);
- menuBar()->addMenu(viewMenu);
- menuBar()->addMenu(helpMenu);
- }
- void ImageViewer::updateActions()
- {
- zoomInAct->setEnabled(!fitToWindowAct->isChecked());
- zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
- normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
- }
- void ImageViewer::scaleImage(double factor)
- {
- Q_ASSERT(imageLabel->pixmap());
- scaleFactor *= factor;
- imageLabel->resize(scaleFactor * imageLabel->pixmap()->size());
- adjustScrollBar(scrollArea->horizontalScrollBar(), factor);
- adjustScrollBar(scrollArea->verticalScrollBar(), factor);
- zoomInAct->setEnabled(scaleFactor < 10.0);
- zoomOutAct->setEnabled(scaleFactor > 0.05);
- }
- void ImageViewer::adjustScrollBar(QScrollBar *scrollBar, double factor)
- {
- scrollBar->setValue(int(factor * scrollBar->value()
- + ((factor - 1) * scrollBar->pageStep()/2)));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement