Warning, file /sdk/ktechlab/src/gui/itemeditor/propertyeditorfile.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /***************************************************************************
0002  *   Copyright (C) 2003 Cedric Pasteur <cedric.pasteur@free.fr>            *
0003  *   Copyright (C) 2006 David Saxton <david@bluehaze.org>                  *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #include "propertyeditorfile.h"
0012 #include "iteminterface.h"
0013 #include "property.h"
0014 
0015 #include <KLineEdit>
0016 #include <KLocalizedString>
0017 
0018 #include <QCursor>
0019 #include <QFileDialog>
0020 #include <QKeyEvent>
0021 #include <QLabel>
0022 #include <QPixmap>
0023 #include <QPushButton>
0024 #include <QResizeEvent>
0025 #include <QString>
0026 #include <QVariant>
0027 
0028 #include <ktechlab_debug.h>
0029 
0030 PropertyEditorFile::PropertyEditorFile(QWidget *parent, Property *property)
0031     : PropertySubEditor(parent, property)
0032 {
0033     m_lineedit = new KLineEdit(this);
0034     m_lineedit->resize(width(), height() - 2);
0035 
0036     m_button = new QPushButton(i18n(" ... "), this);
0037     m_button->resize(height(), height() - 10);
0038     m_button->move(width() - m_button->width() - 1, 1);
0039 
0040     m_lineedit->setText(property->value().toString());
0041     m_lineedit->show();
0042     m_button->show();
0043 
0044     setWidget(m_lineedit);
0045 
0046     connect(m_button, &QPushButton::clicked, this, &PropertyEditorFile::selectFile);
0047     connect(property, qOverload<const QString &>(&Property::valueChanged), m_lineedit, &KLineEdit::setText);
0048 }
0049 
0050 void PropertyEditorFile::selectFile()
0051 {
0052     const QString filePath = QFileDialog::getOpenFileName(this, i18n("Choose File"), QString(), m_property->fileFilters().toQtStyleString());
0053     qCDebug(KTL_LOG) << "got QString: " << filePath;
0054     if (filePath.isEmpty()) {
0055         qCDebug(KTL_LOG) << "url is not valid, not setting it";
0056         return;
0057     }
0058 
0059     m_property->setValue(filePath);
0060     ItemInterface::self()->setProperty(m_property);
0061 }
0062 
0063 void PropertyEditorFile::resizeEvent(QResizeEvent *ev)
0064 {
0065     m_lineedit->resize(ev->size());
0066     m_button->move(ev->size().width() - m_button->width() - 1, 1);
0067 }
0068 
0069 bool PropertyEditorFile::eventFilter(QObject *watched, QEvent *e)
0070 {
0071     if (e->type() == QEvent::KeyPress) {
0072         QKeyEvent *ev = static_cast<QKeyEvent *>(e);
0073         if ((ev->key() == Qt::Key_Enter) || (ev->key() == Qt::Key_Space) || (ev->key() == Qt::Key_Return)) {
0074             m_button->animateClick();
0075             return true;
0076         }
0077     }
0078     return PropertySubEditor::eventFilter(watched, e);
0079 }
0080 
0081 #include "moc_propertyeditorfile.cpp"