summaryrefslogtreecommitdiff
path: root/noncore/graphics/drawpad/importdialog.cpp
blob: a2f4c4072107b11b8f081355bb22bbb0d27d6e68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/***************************************************************************
 *                                                                         *
 *   DrawPad - a drawing program for Opie Environment                      *
 *                                                                         *
 *   (C) 2002 by S. Prud'homme <prudhomme@laposte.net>                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "importdialog.h"

#include <qpe/mimetype.h>

#include <qcheckbox.h>
#include <qimage.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qpushbutton.h>

using namespace Opie::Ui;
ImportDialog::ImportDialog(QWidget* parent, const char* name)
    : QDialog(parent, name, true)
{
    setCaption(tr("DrawPad - Import"));

    MimeTypes types; types.insert( tr("All images"),"image/*" );
    m_pFileSelector = new OFileSelector(this,
                                        OFileSelector::FileSelector,
					OFileSelector::Normal,
					QString::null,
					QString::null, types );
    m_pFileSelector->setNameVisible( false );
    connect(m_pFileSelector, SIGNAL(fileSelected(const DocLnk&)), this, SLOT(fileChanged()));

    m_pPreviewLabel = new QLabel(this);
    m_pPreviewLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    m_pPreviewLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    m_pPreviewLabel->setFixedSize(114, 114);
    m_pPreviewLabel->setBackgroundMode(QWidget::PaletteMid);

    m_pAutomaticPreviewCheckBox = new QCheckBox(tr("Automatic preview"), this);
    m_pAutomaticPreviewCheckBox->setChecked(true);

    QPushButton* previewPushButton = new QPushButton(tr("Preview"), this);
    connect(previewPushButton, SIGNAL(clicked()), this, SLOT(preview()));

    QVBoxLayout* mainLayout = new QVBoxLayout(this, 4, 4);
    QHBoxLayout* previewLayout = new QHBoxLayout(4);
    QVBoxLayout* previewSecondLayout = new QVBoxLayout(4);

    previewSecondLayout->addWidget(m_pAutomaticPreviewCheckBox);
    previewSecondLayout->addWidget(previewPushButton);
    previewSecondLayout->addStretch();

    previewLayout->addWidget(m_pPreviewLabel);
    previewLayout->addLayout(previewSecondLayout);

    mainLayout->addWidget(m_pFileSelector);
    mainLayout->addLayout(previewLayout);

    preview();
}

ImportDialog::~ImportDialog()
{
}

const DocLnk* ImportDialog::selected()
{
    // FIXME change from pointer to reference -zecke
    DocLnk *lnk = new DocLnk( m_pFileSelector->selectedDocument() );
    return lnk;
}

void ImportDialog::fileChanged()
{
    if (m_pAutomaticPreviewCheckBox->isChecked()) {
        preview();
    }
}

void ImportDialog::preview()
{
    const DocLnk* docLnk = selected();

    if (docLnk) {
        QImage image(docLnk->file());

        int previewWidth = m_pPreviewLabel->contentsRect().width();
        int previewHeight = m_pPreviewLabel->contentsRect().height();

        float widthScale = 1.0;
        float heightScale = 1.0;

        if (previewWidth < image.width()) {
            widthScale = (float)previewWidth / float(image.width());
        }

        if (previewHeight < image.height()) {
            heightScale = (float)previewHeight / float(image.height());
        }

        float scale = (widthScale < heightScale ? widthScale : heightScale);
        QImage previewImage = image.smoothScale((int)(image.width() * scale) , (int)(image.height() * scale));

        QPixmap previewPixmap;
        previewPixmap.convertFromImage(previewImage);

        m_pPreviewLabel->setPixmap(previewPixmap);

        delete docLnk;
    }
}