Skip to content

Commit

Permalink
Merge pull request #28 from MartenBE/revert-26-revert-24-modernize-cpp
Browse files Browse the repository at this point in the history
Revert "Revert "Updated pro-file to C++17 and fixed a bunch of warnings from g++, cla…""
  • Loading branch information
abdularis authored Feb 24, 2019
2 parents 220c348 + e406978 commit 3ccbe2c
Show file tree
Hide file tree
Showing 29 changed files with 140 additions and 162 deletions.
32 changes: 15 additions & 17 deletions src/LANShare.pro
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
#
#-------------------------------------------------

QT += core gui network
QT += core gui network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = LANShare
TEMPLATE = app
CONFIG += c++11
CONFIG += c++17

RC_ICONS += img/icon.ico

SOURCES += main.cpp\
settings.cpp \
util.cpp \
singleinstance.cpp \
singleinstance.cpp \
ui/mainwindow.cpp \
ui/receiverselectordialog.cpp \
ui/aboutdialog.cpp \
Expand All @@ -27,15 +27,14 @@ SOURCES += main.cpp\
transfer/sender.cpp \
transfer/transfer.cpp \
transfer/transferserver.cpp \
# transfer/transfermanager.cpp \
model/device.cpp \
model/devicelistmodel.cpp \
model/transferinfo.cpp \
model/transfertablemodel.cpp
model/device.cpp \
model/devicelistmodel.cpp \
model/transferinfo.cpp \
model/transfertablemodel.cpp

HEADERS += settings.h \
HEADERS += settings.h \
util.h \
singleinstance.h \
singleinstance.h \
ui/mainwindow.h \
ui/receiverselectordialog.h \
ui/aboutdialog.h \
Expand All @@ -45,16 +44,15 @@ HEADERS += settings.h \
transfer/sender.h \
transfer/transfer.h \
transfer/transferserver.h \
# transfer/transfermanager.h \
model/device.h \
model/devicelistmodel.h \
model/transferinfo.h \
model/transfertablemodel.h
model/device.h \
model/devicelistmodel.h \
model/transferinfo.h \
model/transfertablemodel.h

FORMS += ui/mainwindow.ui \
FORMS += ui/mainwindow.ui \
ui/receiverselectordialog.ui \
ui/aboutdialog.ui \
ui/settingsdialog.ui

RESOURCES += \
res.qrc
res.qrc
14 changes: 3 additions & 11 deletions src/model/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,14 @@

#include "device.h"

Device::Device()
: mId(""), mName(""), mOSName(""), mAddress(QHostAddress::Null)
Device::Device(const QString &id, const QString &name, const QString &osName, const QHostAddress &addr)
: mId{id}, mName{name}, mOSName{osName}, mAddress{addr}
{
}

bool Device::isValid() const
{
return mId != "" && mName != "" && mOSName != "" && mAddress != QHostAddress::Null;
}

void Device::set(const QString& id, const QString& name, const QString& osName, const QHostAddress& addr)
{
mId = id;
mName = name;
mOSName = osName;
mAddress = addr;
return (mId != "" && mName != "" && mOSName != "" && mAddress != QHostAddress::Null);
}

void Device::setId(const QString& id)
Expand Down
12 changes: 6 additions & 6 deletions src/model/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@
class Device
{
public:
explicit Device();
explicit Device() = default;
Device(const QString &id, const QString &name, const QString &osName, const QHostAddress &addr);

inline QString getId() const { return mId; }
inline QString getName() const { return mName; }
inline QHostAddress getAddress() const { return mAddress; }
inline QString getOSName() const { return mOSName; }
bool isValid() const;

void set(const QString& id, const QString& name, const QString& osName, const QHostAddress& addr);
void setId(const QString& id);
void setName(const QString& name);
void setAddress(const QHostAddress& address);
Expand All @@ -47,10 +47,10 @@ class Device
bool operator!=(const Device& other) const;

private:
QString mId;
QString mName;
QString mOSName;
QHostAddress mAddress;
QString mId{""};
QString mName{""};
QString mOSName{""};
QHostAddress mAddress{QHostAddress::Null};
};

#endif // DEVICE_H
32 changes: 20 additions & 12 deletions src/model/devicelistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,22 @@ DeviceListModel::DeviceListModel(DeviceBroadcaster* deviceBC, QObject* parent)
: QAbstractListModel(parent)
{
mDBC = deviceBC;
if (!mDBC)
if (!mDBC) {
mDBC = new DeviceBroadcaster(this);
}

connect(mDBC, &DeviceBroadcaster::broadcastReceived, this, &DeviceListModel::onBCReceived);
}

void DeviceListModel::onBCReceived(const Device& fromDevice)
void DeviceListModel::onBCReceived(const Device &fromDevice)
{
QString id = fromDevice.getId();
if (id == Settings::instance()->getMyDevice().getId())
if (id == Settings::instance()->getMyDevice().getId()) {
return;
}

bool found = false;
for(Device dev : mDevices) {
for(const Device& dev : mDevices) {
if (dev.getId() == id) {
found = true;
break;
Expand Down Expand Up @@ -81,12 +83,13 @@ QVariant DeviceListModel::data(const QModelIndex &index, int role) const
}
case Qt::DecorationRole : {
QString os = dev.getOSName();
if (os == "Linux")
if (os == "Linux") {
return QPixmap(":/img/linux.png");
else if (os == "Windows")
} else if (os == "Windows") {
return QPixmap(":/img/windows.png");
else if (os == "Mac OSX")
} else if (os == "Mac OSX") {
return QPixmap(":/img/osx.png");
}
}
}

Expand All @@ -110,26 +113,31 @@ void DeviceListModel::refresh()

Device DeviceListModel::device(int index) const
{
if (index < 0 || index >= mDevices.size())
if (index < 0 || index >= mDevices.size()) {
return Device();
}

return mDevices.at(index);
}

Device DeviceListModel::device(const QString &id) const
{
for (Device dev : mDevices)
if (dev.getId() == id)
for (Device dev : mDevices) {
if (dev.getId() == id) {
return dev;
}
}

return Device();
}

Device DeviceListModel::device(const QHostAddress &address) const
{
for (Device dev : mDevices)
if (dev.getAddress() == address)
for (Device dev : mDevices) {
if (dev.getAddress() == address) {
return dev;
}
}

return Device();
}
8 changes: 4 additions & 4 deletions src/model/devicelistmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
class DeviceListModel : public QAbstractListModel
{
public:
DeviceListModel(DeviceBroadcaster* deviceBC, QObject* parent = 0);
DeviceListModel(DeviceBroadcaster* deviceBC, QObject* parent = nullptr);

virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;

void refresh();

Expand All @@ -42,7 +42,7 @@ class DeviceListModel : public QAbstractListModel
void setDevices(const QVector<Device> &getDevices);

private Q_SLOTS:
void onBCReceived(const Device& fromDevice);
void onBCReceived(const Device &fromDevice);

private:
DeviceBroadcaster* mDBC;
Expand Down
6 changes: 4 additions & 2 deletions src/model/transferinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
TransferInfo::TransferInfo(Transfer* owner, QObject *parent) :
QObject(parent),
mState(TransferState::Idle), mLastState(TransferState::Idle),
mType(TransferType::None), mProgress(0),
mType(TransferType::None), mProgress(0), mDataSize(0),
mOwner(owner)
{
}
Expand All @@ -47,7 +47,7 @@ bool TransferInfo::canCancel() const

void TransferInfo::setPeer(Device peer)
{
mPeer = peer;
mPeer = std::move(peer);
}

void TransferInfo::setState(TransferState newState)
Expand Down Expand Up @@ -95,6 +95,8 @@ void TransferInfo::setState(TransferState newState)
}
break;
}
default:
break;
}

mLastState = tmp;
Expand Down
2 changes: 1 addition & 1 deletion src/model/transferinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class TransferInfo : public QObject
Q_OBJECT

public:
explicit TransferInfo(Transfer* owner, QObject *parent = 0);
explicit TransferInfo(Transfer* owner, QObject *parent = nullptr);

inline Device getPeer() const { return mPeer; }
inline int getProgress() const { return mProgress; }
Expand Down
54 changes: 30 additions & 24 deletions src/model/transfertablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ QVariant TransferTableModel::data(const QModelIndex &index, int role) const
case Column::FileSize : return Util::sizeToString(info->getDataSize());
case Column::State : return getStateString(info->getState());
case Column::Progress : return info->getProgress();
default : break;
}
}
else if (role == Qt::ForegroundRole && col == Column::State) {
Expand All @@ -80,6 +81,7 @@ QVariant TransferTableModel::headerData(int section, Qt::Orientation orientation
case Column::FileSize : return tr("Size");
case Column::State : return tr("Status");
case Column::Progress : return tr("Progress");
default : break;
}
}

Expand All @@ -88,28 +90,30 @@ QVariant TransferTableModel::headerData(int section, Qt::Orientation orientation

void TransferTableModel::insertTransfer(Transfer *t)
{
if (t) {
beginInsertRows(QModelIndex(), 0, 0);
mTransfers.prepend(t);
endInsertRows();
emit dataChanged(index(1, 0), index(mTransfers.size()-1, (int) Column::Count));

TransferInfo* info = t->getTransferInfo();
connect(info, &TransferInfo::fileOpened, [=]() {
int idx = mTransfers.indexOf(info->getOwner());
QModelIndex fNameIdx = index(idx, (int) Column::FileName);
QModelIndex fSizeIdx = index(idx, (int) Column::FileSize);
emit dataChanged(fNameIdx, fSizeIdx);
});

connect(info, &TransferInfo::stateChanged, [=](TransferState state) {
Q_UNUSED(state);

int idx = mTransfers.indexOf(info->getOwner());
QModelIndex stateIdx = index(idx, (int) Column::State);
emit dataChanged(stateIdx, stateIdx);
});
if (!t) {
return;
}

beginInsertRows(QModelIndex(), 0, 0);
mTransfers.prepend(t);
endInsertRows();
emit dataChanged(index(1, 0), index(mTransfers.size()-1, (int) Column::Count));

TransferInfo* info = t->getTransferInfo();
connect(info, &TransferInfo::fileOpened, [=]() {
int idx = mTransfers.indexOf(info->getOwner());
QModelIndex fNameIdx = index(idx, (int) Column::FileName);
QModelIndex fSizeIdx = index(idx, (int) Column::FileSize);
emit dataChanged(fNameIdx, fSizeIdx);
});

connect(info, &TransferInfo::stateChanged, [=](TransferState state) {
Q_UNUSED(state);

int idx = mTransfers.indexOf(info->getOwner());
QModelIndex stateIdx = index(idx, (int) Column::State);
emit dataChanged(stateIdx, stateIdx);
});
}

void TransferTableModel::clearCompleted()
Expand All @@ -133,8 +137,9 @@ void TransferTableModel::clearCompleted()

Transfer* TransferTableModel::getTransfer(int index) const
{
if (index < 0 || index >= mTransfers.size())
return NULL;
if (index < 0 || index >= mTransfers.size()) {
return nullptr;
}

return mTransfers.at(index);
}
Expand All @@ -146,8 +151,9 @@ TransferInfo* TransferTableModel::getTransferInfo(int index) const

void TransferTableModel::removeTransfer(int index)
{
if (index < 0 || index >= mTransfers.size())
if (index < 0 || index >= mTransfers.size()) {
return;
}

beginRemoveRows(QModelIndex(), index, index);
mTransfers.at(index)->deleteLater();
Expand Down
12 changes: 6 additions & 6 deletions src/model/transfertablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ class TransferTableModel : public QAbstractTableModel
Q_OBJECT

public:
explicit TransferTableModel(QObject *parent = 0);
~TransferTableModel();
explicit TransferTableModel(QObject *parent = nullptr);
~TransferTableModel() override;

virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override;
virtual int columnCount(const QModelIndex &parent = QModelIndex()) const override;
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;

void insertTransfer(Transfer* t);
void clearCompleted();
Expand Down
Loading

0 comments on commit 3ccbe2c

Please sign in to comment.