Skip to content

Commit

Permalink
More code
Browse files Browse the repository at this point in the history
  • Loading branch information
dangelog committed Apr 22, 2021
1 parent e1f02ce commit 9a877e6
Show file tree
Hide file tree
Showing 7 changed files with 347 additions and 0 deletions.
97 changes: 97 additions & 0 deletions Qt-Widgets-and-more/WidgetPromotion/FileSelector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/* MIT License
Copyright (C) 2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "FileSelector.h"
#include <QFileDialog>
#include <QLayout>
#include <QLineEdit>
#include <QToolButton>

FileSelector::FileSelector(QWidget *parent)
: QWidget(parent)
{
auto *layout = new QHBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);

m_lineEdit = new QLineEdit(this);
layout->addWidget(m_lineEdit);

connect(m_lineEdit, &QLineEdit::editingFinished, this, &FileSelector::emitPathChanged);

auto *button = new QToolButton;
button->setText("...");
layout->addWidget(button);

connect(button, &QToolButton::clicked, this, &FileSelector::chooseFile);

setFocusProxy(m_lineEdit);
}

void FileSelector::setMode(Mode mode)
{
if (mode != m_mode) {
m_mode = mode;
emit modeChanged(mode);
}
}

FileSelector::Mode FileSelector::mode() const
{
return m_mode;
}

void FileSelector::setPath(const QString &fileName)
{
m_lineEdit->setText(fileName);
m_lastEmittedPath = fileName;
}

QString FileSelector::path() const
{
return m_lineEdit->text();
}

void FileSelector::chooseFile()
{
QString fileName;
if (mode() == FileOpen)
fileName = QFileDialog::getOpenFileName(this, QString(), m_lineEdit->text());
else if (mode() == FileSave)
fileName = QFileDialog::getSaveFileName(this, QString(), m_lineEdit->text());
else
fileName = QFileDialog::getExistingDirectory(this, QString(), m_lineEdit->text());

if (!fileName.isEmpty()) {
m_lineEdit->setText(fileName);
emitPathChanged();
}
}

void FileSelector::emitPathChanged()
{
QString newPath = m_lineEdit->text();
if (newPath != m_lastEmittedPath) {
m_lastEmittedPath = newPath;
emit pathChanged(newPath);
}
}
59 changes: 59 additions & 0 deletions Qt-Widgets-and-more/WidgetPromotion/FileSelector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* MIT License
Copyright (C) 2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#pragma once

#include <QWidget>

class QLineEdit;

class FileSelector : public QWidget
{
Q_OBJECT
Q_PROPERTY(Mode mode READ mode WRITE setMode NOTIFY modeChanged)
Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged)

public:
explicit FileSelector(QWidget *parent = nullptr);

QString path() const;
void setPath(const QString &path);

enum Mode { FileOpen, FileSave, Directory };
Mode mode() const;
void setMode(Mode mode);

signals:
void pathChanged(const QString &path);
void modeChanged(Mode mode);

private slots:
void chooseFile();

private:
void emitPathChanged();

QLineEdit *m_lineEdit;
Mode m_mode = FileSave;
QString m_lastEmittedPath;
};
42 changes: 42 additions & 0 deletions Qt-Widgets-and-more/WidgetPromotion/ImageViewer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* MIT License
Copyright (C) 2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "ImageViewer.h"
#include "ui_ImageViewer.h"

ImageViewer::ImageViewer(QWidget *parent)
: QWidget(parent)
, ui(new Ui::ImageViewer)
{
ui->setupUi(this);
ui->fileSelector->setMode(FileSelector::FileOpen);
connect(ui->fileSelector, &FileSelector::pathChanged, [this](const QString &fileName) {
QPixmap image = QPixmap(fileName).scaled(600, 600, Qt::KeepAspectRatio);
ui->image->setPixmap(image);
});
}

ImageViewer::~ImageViewer()
{
delete ui;
}
42 changes: 42 additions & 0 deletions Qt-Widgets-and-more/WidgetPromotion/ImageViewer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* MIT License
Copyright (C) 2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#pragma once

#include <QWidget>

namespace Ui {
class ImageViewer;
}

class ImageViewer : public QWidget
{
Q_OBJECT

public:
explicit ImageViewer(QWidget *parent = nullptr);
~ImageViewer();

private:
Ui::ImageViewer *ui;
};
47 changes: 47 additions & 0 deletions Qt-Widgets-and-more/WidgetPromotion/ImageViewer.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ImageViewer</class>
<widget class="QWidget" name="ImageViewer">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Image Viewer</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout" stretch="0,1">
<item>
<widget class="FileSelector" name="fileSelector" native="true"/>
</item>
<item>
<widget class="QLabel" name="image">
<property name="font">
<font>
<pointsize>24</pointsize>
</font>
</property>
<property name="text">
<string>Load an image.</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>FileSelector</class>
<extends>QWidget</extends>
<header>FileSelector.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
26 changes: 26 additions & 0 deletions Qt-Widgets-and-more/WidgetPromotion/WidgetPromotion.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
FileSelector.cpp \
ImageViewer.cpp \
main.cpp

HEADERS += \
FileSelector.h \
ImageViewer.h

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

FORMS += \
ImageViewer.ui
34 changes: 34 additions & 0 deletions Qt-Widgets-and-more/WidgetPromotion/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* MIT License
Copyright (C) 2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "ImageViewer.h"

#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ImageViewer viewer;
viewer.show();
return a.exec();
}

0 comments on commit 9a877e6

Please sign in to comment.