#include <QPrinter>
#include <QPrintDialog>
#include <QX11Info>
#include <QFileDialog>
#include <QPixmap>
#include <QPainter>
#include "QPMain.h"

QPMain::QPMain() : printer(NULL), printDialog(NULL), image(NULL) {
   ui.setupUi(this);
   ui.statusbar->show();
   connect(ui.actionQuit, SIGNAL(triggered()), qApp, SLOT(quit()));
   connect(ui.actionPrintSetup, SIGNAL(triggered()), this, SLOT(printSetup()));
   connect(ui.actionPrint, SIGNAL(triggered()), this, SLOT(print()));
   connect(ui.actionSaveConfig, SIGNAL(triggered()), this, SLOT(saveConfig()));
   connect(ui.actionLoadConfig, SIGNAL(triggered()), this, SLOT(loadConfig()));
   connect(ui.actionLoadImage, SIGNAL(triggered()), this, SLOT(loadImage()));
   image = new QPixmap(":/images/big-logo.png");
   ui.imageLabel->setPixmap(*image);
   ui.imageLabel->setMinimumSize(QSize(1,1));
}

QPMain::~QPMain() {
}

void
QPMain::printSetup() {
   if (printer == NULL) {
      printer = new QPrinter(QPrinter::HighResolution);
   }
   if (printDialog == NULL) {
      printDialog = new QPrintDialog(printer, this);
   }
   if (printDialog->exec() == QDialog::Accepted) {
      ui.imageLabel->clear();
      QPrinter * p = printDialog->printer();
      QRectF r = p->pageRect(QPrinter::Inch);
      SetViewSize(r.width(), r.height());
   }
}

void
QPMain::print()
{
   if (printer != NULL) {
      QPainter painter(printer);
      QRect rect = painter.viewport();
      QSize size = image->size();
      size.scale(rect.size(), Qt::IgnoreAspectRatio);
      painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
      painter.setWindow(image->rect());
      painter.drawPixmap(0, 0, *image);
   }
}

void
QPMain::saveConfig() {
   QString config_file =
      QFileDialog::getSaveFileName(this, "Save Config",
                                   QString(),
                                   "Configs (*.qp)");
   if (! config_file.isEmpty()) {
      // PUT CODE HERE
   }
}

void
QPMain::loadConfig() {
   QString config_file =
      QFileDialog::getOpenFileName(this, "Load Config",
                                   QString(),
                                   "Configs (*.qp)");
   if (! config_file.isEmpty()) {
      // PUT CODE HERE
   }
}

void
QPMain::loadImage() {
   QString image_file =
      QFileDialog::getOpenFileName(this, "Load Image",
                                   QString(),
                                   "Images (*.png *.xpm *.jpg)");
   if (! image_file.isEmpty()) {
      if (image != NULL) {
         delete image;
      }
      image = new QPixmap(image_file);
      ui.imageLabel->setPixmap(
            image->scaled(ui.imageLabel->width(), ui.imageLabel->height(),
                          Qt::IgnoreAspectRatio,
                          Qt::SmoothTransformation));
      ui.imageLabel->setMinimumSize(QSize(1,1));
   }
}

void
QPMain::SetViewSize(qreal width_inch, qreal height_inch) {

   // First compute the pixel width and height I want the central
   // widget to have.

   QX11Info xi;
   int dpi_width = xi.appDpiX(xi.screen());
   int dpi_height = xi.appDpiY(xi.screen());
   int pix_width = (int)((((qreal)dpi_width) * width_inch) + 0.5);
   int pix_height = (int)((((qreal)dpi_height) * height_inch) + 0.5);

   int grow_width = pix_width - ui.imageLabel->width();
   int grow_height = pix_height - ui.imageLabel->height();

   ui.imageLabel->clear();
   ui.imageLabel->setFixedSize(QSize(pix_width, pix_height));
   ui.imageLabel->setMinimumSize(QSize(1,1));

   if (image != NULL) {
      ui.imageLabel->setPixmap(image->scaled(pix_width, pix_height,
                               Qt::IgnoreAspectRatio,
                               Qt::SmoothTransformation));
   }

   // Now find the current width and height of the central widget
   // and change the size of the main window by the difference in size
   // of the central widget (which should have the side effect of leaving
   // the central widget the size we want).

   resize(width() + grow_width, height() + grow_height);
}
