Skip to content

Commit

Permalink
Add ability to change tray icon.
Browse files Browse the repository at this point in the history
  • Loading branch information
martinrotter authored and palinek committed Dec 12, 2016
1 parent 4e63b8b commit b832822
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 33 deletions.
5 changes: 3 additions & 2 deletions src/main.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <mutex>

#include "qlippersystray.h"
#include "qlipperpreferences.h"

int main(int argc, char **argv)
{
Expand Down Expand Up @@ -69,14 +70,14 @@ int main(int argc, char **argv)
a.setApplicationVersion(QLIPPER_VERSION);
a.setOrganizationDomain("qlipper.org");
a.setOrganizationName("Qlipper");
a.setWindowIcon(QIcon(":/icons/qlipper.png"));

QSettings::setDefaultFormat(QSettings::IniFormat);

// for QByteArray to QString constructors
// QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));

a.setQuitOnLastWindowClosed(false);

a.setWindowIcon(QIcon(QlipperPreferences::Instance()->getPathToIcon()));

// potentially load translator
QString fname(a.applicationName() + "_" + QLocale::system().name());
Expand Down
15 changes: 14 additions & 1 deletion src/qlipperpreferences.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#include "qlipperpreferences.h"

const QString QlipperPreferences::DEFAULT_ICON_PATH = QStringLiteral(":/icons/qlipper.png");


// allow to store ClipboardContent in the QSettings variant
QDataStream &operator<<(QDataStream &out, const ClipboardContent &obj)
{
Expand Down Expand Up @@ -164,6 +167,17 @@ void QlipperPreferences::saveDynamicItems(QList<QlipperItem> list)
sync();
}

QString QlipperPreferences::getPathToIcon() const
{
return value(QLatin1String("tray_icon_file"), DEFAULT_ICON_PATH).toString();
}

void QlipperPreferences::savePathToIcon(const QString &path)
{
setValue(QLatin1String("tray_icon_file"), path);
sync();
}

bool QlipperPreferences::trim()
{
return value("trim", true).toBool();
Expand Down Expand Up @@ -204,7 +218,6 @@ bool QlipperPreferences::synchronizeHistory() const
return value("synchronizeHistory", true).toBool();
}


bool QlipperPreferences::networkSend() const
{
return value("networkSend", false).toBool();
Expand Down
6 changes: 5 additions & 1 deletion src/qlipperpreferences.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <QDataStream>
#include "qlipperitem.h"


class QlipperPreferences : public QSettings
{
public:
Expand All @@ -36,6 +35,8 @@ class QlipperPreferences : public QSettings
};

public:
static const QString DEFAULT_ICON_PATH;

static QlipperPreferences *Instance();
~QlipperPreferences();

Expand All @@ -44,6 +45,9 @@ class QlipperPreferences : public QSettings
QList<QlipperItem> getDynamicItems();
void saveDynamicItems(QList<QlipperItem> list);

QString getPathToIcon() const;
void savePathToIcon(const QString &path);

bool trim();
int displaySize() const;
QString shortcut() const;
Expand Down
57 changes: 57 additions & 0 deletions src/qlipperpreferencesdialog.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#include <QInputDialog>

#include <QFileDialog>
#include <QMenu>

#include "qlipperitem.h"
#include "qlipperpreferences.h"
#include "qlipperpreferencesdialog.h"
Expand Down Expand Up @@ -73,6 +76,22 @@ QlipperPreferencesDialog::QlipperPreferencesDialog(QWidget *parent) :
stickyRemoveButton->setEnabled(false);
}

// Setup icon image machinery.
// Setup menu & actions for icon selection.
QMenu *iconMenu = new QMenu(tr("Icon selection"), this);
QAction *actLoadFromFile = new QAction(tr("Load icon from file..."), this);
QAction *actUseDefault = new QAction(QIcon(QlipperPreferences::DEFAULT_ICON_PATH), tr("Use default icon"), this);

iconMenu->addAction(actLoadFromFile);
iconMenu->addAction(actUseDefault);

temporarilyRembemberNewTrayIcon(s->getPathToIcon());

buttonIconImage->setMenu(iconMenu);

connect(actLoadFromFile, &QAction::triggered, this, &QlipperPreferencesDialog::selectIconFromFile);
connect(actUseDefault, &QAction::triggered, this, &QlipperPreferencesDialog::useDefaultIcon);

resize(sizeHint());
}

Expand Down Expand Up @@ -100,9 +119,37 @@ void QlipperPreferencesDialog::accept()
}
QlipperPreferences::Instance()->saveStickyItems(list);

QlipperPreferences::Instance()->savePathToIcon(getNewTrayIcon());

QDialog::accept();
}

void QlipperPreferencesDialog::selectIconFromFile()
{
QScopedPointer<QFileDialog> dialog(new QFileDialog(this,
tr("Select icon file"),
QString(),
tr("Images (*.bmp *.jpg *.jpeg *.png *.svg *.tga)")));
dialog->setFileMode(QFileDialog::ExistingFile);
dialog->setOptions(QFileDialog::ReadOnly);
dialog->setViewMode(QFileDialog::Detail);
dialog->setLabelText(QFileDialog::Accept, tr("Select icon"));
dialog->setLabelText(QFileDialog::Reject, tr("Cancel"));
//: Label to describe the folder for icon file selection dialog.
dialog->setLabelText(QFileDialog::LookIn, tr("Look in:"));
dialog->setLabelText(QFileDialog::FileName, tr("Icon name:"));
dialog->setLabelText(QFileDialog::FileType, tr("Icon type:"));

if (dialog->exec() == QDialog::Accepted && !dialog->selectedFiles().isEmpty()) {
temporarilyRembemberNewTrayIcon(dialog->selectedFiles().value(0));
}
}

void QlipperPreferencesDialog::useDefaultIcon()
{
temporarilyRembemberNewTrayIcon(QlipperPreferences::DEFAULT_ICON_PATH);
}

void QlipperPreferencesDialog::stickyAddButton_clicked()
{
bool ok;
Expand Down Expand Up @@ -157,3 +204,13 @@ void QlipperPreferencesDialog::listWidget_currentRowChanged(int row)
stickyDownButton->setEnabled(count && row != count-1);
stickyRemoveButton->setEnabled(count);
}

void QlipperPreferencesDialog::temporarilyRembemberNewTrayIcon(const QString &path) {
buttonIconImage->setIcon(QIcon(path));
buttonIconImage->setProperty("tray_icon", path);
}

QString QlipperPreferencesDialog::getNewTrayIcon() const
{
return buttonIconImage->property("tray_icon").toString();
}
7 changes: 7 additions & 0 deletions src/qlipperpreferencesdialog.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,18 @@ class QlipperPreferencesDialog : public QDialog, private Ui::QlipperPreferencesD
private slots:
void accept();

void selectIconFromFile();
void useDefaultIcon();

void stickyAddButton_clicked();
void stickyRemoveButton_clicked();
void stickyUpButton_clicked();
void stickyDownButton_clicked();
void listWidget_currentRowChanged(int);

private:
void temporarilyRembemberNewTrayIcon(const QString &path);
QString getNewTrayIcon() const;
};

#endif // QLIPPERPREFERENCESDIALOG_H
88 changes: 60 additions & 28 deletions src/qlipperpreferencesdialog.ui
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>358</width>
<height>347</height>
<height>430</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -35,7 +35,7 @@
<x>0</x>
<y>0</y>
<width>340</width>
<height>217</height>
<height>302</height>
</rect>
</property>
<attribute name="label">
Expand Down Expand Up @@ -107,25 +107,47 @@
</property>
</widget>
</item>
<item row="3" column="0" colspan="2">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="keyboardShortcutLabel">
<property name="text">
<string>Keyboard Shortcut:</string>
</property>
</widget>
</item>
<item>
<widget class="QKeySequenceWidget" name="shortcutWidget" native="true">
<property name="toolTip">
<string>Change global keyboard shortcut to invoke the menu on screen</string>
</property>
</widget>
</item>
</layout>
<item row="3" column="0">
<widget class="QLabel" name="keyboardShortcutLabel">
<property name="text">
<string>Keyboard Shortcut:</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QKeySequenceWidget" name="shortcutWidget" native="true">
<property name="toolTip">
<string>Change global keyboard shortcut to invoke the menu on screen</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Tray icon image:</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QToolButton" name="buttonIconImage">
<property name="text">
<string/>
</property>
<property name="iconSize">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
<property name="popupMode">
<enum>QToolButton::InstantPopup</enum>
</property>
</widget>
</item>
<item row="4" column="0" colspan="2">
<item row="5" column="0" colspan="2">
<widget class="QCheckBox" name="platformExtensionsCheckBox">
<property name="toolTip">
<string>Use clipboard extensions (X11 Selections, OS X Find Buffer) when it's supported</string>
Expand All @@ -135,7 +157,7 @@
</property>
</widget>
</item>
<item row="5" column="0" colspan="2">
<item row="6" column="0" colspan="2">
<widget class="QComboBox" name="synchronizePSE">
<item>
<property name="text">
Expand All @@ -154,21 +176,21 @@
</item>
</widget>
</item>
<item row="6" column="0" colspan="2">
<item row="7" column="0" colspan="2">
<widget class="QCheckBox" name="clearItemsOnExit">
<property name="text">
<string>Clear Items on Exit</string>
</property>
</widget>
</item>
<item row="7" column="0" colspan="2">
<item row="8" column="0" colspan="2">
<widget class="QCheckBox" name="synchronizeHistory">
<property name="text">
<string>Synchronize history to storage instantly</string>
</property>
</widget>
</item>
<item row="8" column="0" colspan="2">
<item row="9" column="0" colspan="2">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
Expand All @@ -188,8 +210,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>174</width>
<height>136</height>
<width>340</width>
<height>302</height>
</rect>
</property>
<attribute name="label">
Expand Down Expand Up @@ -279,8 +301,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>232</width>
<height>99</height>
<width>340</width>
<height>302</height>
</rect>
</property>
<attribute name="label">
Expand Down Expand Up @@ -390,6 +412,16 @@
<signal>clicked(bool)</signal>
<receiver>synchronizePSE</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>20</x>
<y>20</y>
</hint>
<hint type="destinationlabel">
<x>20</x>
<y>20</y>
</hint>
</hints>
</connection>
</connections>
</ui>
6 changes: 5 additions & 1 deletion src/qlippersystray.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ QlipperSystray::QlipperSystray(QObject *parent)
, m_shortcutMenu(0)
#endif
{
setIcon(QIcon(":/icons/qlipper.png"));
setIcon(QIcon(QlipperPreferences::Instance()->getPathToIcon()));

m_model = new QlipperModel(this);

Expand Down Expand Up @@ -108,6 +108,10 @@ void QlipperSystray::editPreferences()
m_shortcut->setShortcut(QlipperPreferences::Instance()->shortcut());
#endif
m_model->resetPreferences();

// Set new icon.
const QString icon_path = QlipperPreferences::Instance()->getPathToIcon();
setIcon(QIcon(icon_path));
}

void QlipperSystray::showAbout()
Expand Down

0 comments on commit b832822

Please sign in to comment.