22#include "pixmapedit.h"
25#include "KPropertyUtils.h"
26#include "KPropertyUtils_p.h"
28#include <QApplication>
32#include <QDesktopWidget>
37#include <QResizeEvent>
43static const int POPUP_MARGIN = 2;
45class Q_DECL_HIDDEN KPropertyPixmapEditor::Private
63 QPixmap previewPixmap;
66KPropertyPixmapEditor::KPropertyPixmapEditor(
KProperty *prop,
QWidget *parent)
67 :
QWidget(parent), d(new Private)
75 d->edit =
new QLabel(
this);
77 d->edit->setContentsMargins(0, 1, 0, 0);
78 d->edit->setToolTip(tr(
"Click to show image preview"));
82 d->edit->setMouseTracking(
true);
86 KPropertyUtils::setupDotDotDotButton(d->button, tr(
"Insert image from file"),
87 tr(
"Inserts image from file"));
93 d->popup->setMargin(POPUP_MARGIN);
94 d->popup->setLineWidth(1);
97 setFocusProxy(d->edit);
98 connect(d->button, SIGNAL(clicked()),
this, SLOT(selectPixmap()));
100 d->edit->installEventFilter(
this);
101 installEventFilter(
this);
104KPropertyPixmapEditor::~KPropertyPixmapEditor()
109QVariant KPropertyPixmapEditor::value()
const
114void KPropertyPixmapEditor::setValue(
const QVariant &value)
116 d->pixmap = value.
value<QPixmap>();
117 if (d->pixmap.isNull() || (d->pixmap.height() <=
height())) {
118 d->previewPixmap = d->pixmap;
120 QImage img(d->pixmap.toImage());
121 const QSize sz(
size() - QSize(0,1));
122 if (!QRect(QPoint(0, 0), sz).contains(d->pixmap.rect())) {
126 d->previewPixmap = d->pixmap;
129 emit commitData(
this);
132QString KPropertyPixmapEditor::selectPixmapFileName()
141 const QString caption(
142 tr(
"Insert Image From File (for \"%1\" property)").arg(d->property->captionOrName()));
152 QString fileName = url.isLocalFile() ? url.toLocalFile() : url.toString();
159void KPropertyPixmapEditor::selectPixmap()
161 const QString fileName(selectPixmapFileName());
166 if (!pm.
load(fileName)) {
184 const QRect screenRect = QApplication::desktop()->availableGeometry(editor);
186 int aboveSpace = std::min(pixmapSize.
height(), globalRect.
y() - screenRect.
top());
187 int belowSpace = std::min(pixmapSize.
height(), screenRect.
bottom() - globalRect.
bottom());
192 std::min(pixmapSize.
width(), screenRect.
right() - globalRect.
left()),
193 std::min(pixmapSize.
width(), screenRect.
right() - globalRect.
left()),
194 std::min(pixmapSize.
width(), globalRect.
right() - screenRect.
left()),
195 std::min(pixmapSize.
width(), globalRect.
right() - screenRect.
left())
197 const std::vector<int> areas{ widths[0] * belowSpace, widths[1] * aboveSpace,
198 widths[2] * belowSpace, widths[3] * aboveSpace };
199 const int areaNumber = std::distance(areas.begin(), std::max_element(areas.begin(), areas.end()));
201 switch (areaNumber) {
203 int width = double(belowSpace) / pixmapSize.
height() * pixmapSize.
width();
204 if (width > widths[areaNumber]) {
205 width = widths[areaNumber];
206 belowSpace = double(width) / pixmapSize.
width() * pixmapSize.
height();
208 rect =
QRect(globalRect.
left(), globalRect.
bottom(), width, belowSpace);
212 int width = double(aboveSpace) / pixmapSize.
height() * pixmapSize.
width();
213 if (width > widths[areaNumber]) {
214 width = widths[areaNumber];
215 aboveSpace = double(width) / pixmapSize.
width() * pixmapSize.
height();
217 rect =
QRect(globalRect.
left(), globalRect.
top() - aboveSpace, width, aboveSpace);
221 int width = double(belowSpace) / pixmapSize.
height() * pixmapSize.
width();
222 if (width > widths[areaNumber]) {
223 width = widths[areaNumber];
224 belowSpace = double(width) / pixmapSize.
width() * pixmapSize.
height();
226 rect =
QRect(globalRect.
right() - width, globalRect.
bottom(), width, belowSpace);
230 int width = double(aboveSpace) / pixmapSize.
height() * pixmapSize.
width();
231 if (width > widths[areaNumber]) {
232 width = widths[areaNumber];
233 aboveSpace = double(width) / pixmapSize.
width() * pixmapSize.
height();
235 rect =
QRect(globalRect.
right() - width, globalRect.
top() - aboveSpace, width,
248 if (d->pixmap.height() <= d->edit->height() && d->pixmap.width() <= d->edit->width()) {
251 d->popup->setGeometry(popupGeometry(
this, d->pixmap.size()));
252 d->popup->setPixmap(d->pixmap);
255 if (d->popup->isVisible()) {
259 QKeyEvent* e =
static_cast<QKeyEvent*
>(ev);
261 d->button->animateClick();
265 }
else if (o ==
this) {
267 d->button->setMaximumWidth(
height());
282KPropertyPixmapDelegate::KPropertyPixmapDelegate()
286QWidget* KPropertyPixmapDelegate::createEditor(
int type,
QWidget *parent,
292 KProperty *
property = KPropertyUtils::propertyForIndex(index);
296 KPropertyPixmapEditor *pe =
new KPropertyPixmapEditor(property, parent);
300void KPropertyPixmapDelegate::paint(
QPainter * painter,
303 const KPropertyUtilsPrivate::PainterSaver saver(painter);
306 if (pm.
height() > option.rect.height() || pm.
width() > option.rect.width()) {
321 painter->
drawPixmap(option.rect.topLeft().x(),
322 option.rect.topLeft().y() + (option.rect.height() - pm.
height()) / 2, pm);
324 QRect r(option.rect);
325 r.setLeft(r.left() + pm.
width() + 2);
331 const QPixmap pm(value.
value<QPixmap>());
The base class representing a single property.
QUrl getOpenFileUrl(QWidget *parent, const QString &caption, const QUrl &dir, const QString &filter, QString *selectedFilter, Options options, const QStringList &supportedSchemes)
void setContentsMargins(const QMargins &margins)
Language language() const const
QString toString(QDate date, FormatType format) const const
QVariant data(int role) const const
virtual bool eventFilter(QObject *watched, QEvent *event)
QVariant property(const char *name) const const
QString tr(const char *sourceText, const char *disambiguation, int n)
void drawPixmap(const QPoint &point, const QPixmap &pixmap)
void drawText(const QPoint &position, const QString &text)
QPixmap fromImage(QImage &&image, Qt::ImageConversionFlags flags)
bool isNull() const const
bool load(const QString &fileName, const char *format, Qt::ImageConversionFlags flags)
QImage toImage() const const
QString arg(Args &&... args) const const
QString fromLatin1(QByteArrayView str)
bool isEmpty() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)