Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sticky notes plugin #1006

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,12 @@ if(SPACER_PLUGIN)
add_subdirectory(plugin-spacer)
endif()

setByDefault(NOTES_PLUGIN Yes)
if(NOTES_PLUGIN)
list(APPEND ENABLED_PLUGINS "Notes")
add_subdirectory(plugin-notes)
endif()

#########################################################################

message(STATUS "**************** The following plugins will be built ****************")
Expand Down
28 changes: 28 additions & 0 deletions plugin-notes/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
set(PLUGIN "notes")

set(HEADERS
notes.h
stickynote.h
notesconfiguration.h
)

set(SOURCES
notes.cpp
stickynote.cpp
notesconfiguration.cpp
)

set(UIS
stickynote.ui
notesconfiguration.ui
)

set(RESOURCES
resources.qrc
)

set(LIBRARIES
lxqt
)

BUILD_LXQT_PLUGIN(${PLUGIN})
225 changes: 225 additions & 0 deletions plugin-notes/notes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
/* BEGIN_COMMON_COPYRIGHT_HEADER
* (c)LGPL2+
*
* LXQt - a lightweight, Qt based, desktop toolset
* https://lxqt.org
*
* Copyright: 2012 Razor team
* Authors:
* Matteo Fois <[email protected]>
*
* This program or library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* END_COMMON_COPYRIGHT_HEADER */

#include "notes.h"
#include "notesconfiguration.h"
#include "stickynote.h"

#include <QMouseEvent>
#include <QResizeEvent>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QScreen>
#include <QStandardPaths>
#include <QSettings>
#include <QLabel>
#include <QDateTime>
#include <QMessageBox>
#include <QDir>
#include <QFontDialog>
#include <QAction>
#include <QMenu>
#include <QDebug>

Notes::Notes(const ILXQtPanelPluginStartupInfo &startupInfo) :
QObject(),
ILXQtPanelPlugin(startupInfo),
mHidden(false)
{
realign();

QAction *addNew = new QAction(tr("New Note"));
QAction *showHide = new QAction(tr("Show/Hide Notes"));

connect(addNew, &QAction::triggered, this, &Notes::addNewNote);
connect(showHide, &QAction::triggered, this, &Notes::toggleShowHide);

QMenu *menu = new QMenu;
menu->addAction(addNew);
menu->addAction(showHide);

mButton.setMenu(menu);
mButton.setPopupMode(QToolButton::InstantPopup);
mButton.setAutoRaise(true);
mButton.setIcon(XdgIcon::fromTheme("date", "date"));


// load all notes
QDir dir(dataDir());
QStringList notes = dir.entryList(QDir::Files);

for(int i = 0; i < notes.size(); ++i) {
qint64 noteId = notes[i].toLong();

if(!noteId)
continue;

StickyNote *note = new StickyNote(noteId, &window);
connect(note, &StickyNote::deleteRequested, this, &Notes::deleteNote);

mNotes[noteId] = note;
}

// set colors & fonts
settingsChanged();
}


Notes::~Notes()
{
for(auto it = mNotes.begin(); it != mNotes.end(); ++it)
delete it.value();

mNotes.clear();
}

void Notes::realign()
{
mButton.setFixedHeight(panel()->iconSize());
mButton.setFixedWidth(panel()->iconSize());
}


QString Notes::dataDir()
{
QString dir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QDir::separator() + "lxqt-notes";
return dir;
}

void Notes::addNewNote()
{
StickyNote *note = new StickyNote(0, &window);
connect(note, &StickyNote::deleteRequested, this, &Notes::deleteNote);

QFont font = qvariant_cast<QFont>(settings()->value("defaultFont"));
QString bgColor = settings()->value("backgroundColor", "#ffff7f").toString();
QString fgColor = settings()->value("foregroundColor", "#000000").toString();
bool showFrame = settings()->value("showWindowFrame", "false").toBool();

note->setFont(font);
note->setColors(bgColor, fgColor);

if(!showFrame)
note->setWindowFlags(Qt::Window | Qt::Tool | Qt::FramelessWindowHint);
else
note->setWindowFlags(Qt::Window | Qt::Tool);

mNotes[note->id()] = note;
note->show();
}

void Notes::deleteNote(const qint64 &id)
{
auto it = mNotes.find(id);
if(it != mNotes.end()) {
delete it.value();
mNotes.erase(it);
}

// delete note file
QDir dir(dataDir());
dir.remove(QString::number(id));
}

void Notes::toggleShowHide()
{
if(mHidden) {
for(auto it = mNotes.begin(); it != mNotes.end(); ++it)
it.value()->show();
mHidden = false;
} else {
for(auto it = mNotes.begin(); it != mNotes.end(); ++it)
it.value()->hide();
mHidden = true;
}
}

void Notes::setIconColor(const QString &icon, const QString &color)
{
// create icons path
QString iconsDir = dataDir() + QDir::separator() + "icons";
QDir().mkpath(iconsDir);

// set icon color
QFile file(icon);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;

QTextStream in(&file);
QString svg = in.readAll().arg(color);

file.close();

QFileInfo info(icon);
QString iconFile = iconsDir + QDir::separator() + info.fileName();

QFile outFile(iconFile);
if(!outFile.open(QIODevice::WriteOnly | QIODevice::Text))
return;

QTextStream out(&outFile);
out << svg;

outFile.close();
}

void Notes::settingsChanged()
{
// new params
QFont font = qvariant_cast<QFont>(settings()->value("defaultFont"));
QString bgColor = settings()->value("backgroundColor", "#ffff7f").toString();
QString fgColor = settings()->value("foregroundColor", "#000000").toString();
bool showFrame = settings()->value("showWindowFrame","false").toBool();
mHidden = !(settings()->value("showNotesOnStartup","false").toBool());

setIconColor(":/resources/times-solid.svg", fgColor);
setIconColor(":/resources/font-solid.svg", fgColor);

for(auto it = mNotes.begin(); it != mNotes.end(); ++it) {
StickyNote *note = it.value();

if(!note->hasOwnFont())
note->setFont(font);

note->setColors(bgColor, fgColor);

if(!showFrame)
note->setWindowFlags(Qt::Window | Qt::Tool | Qt::FramelessWindowHint);
else
note->setWindowFlags(Qt::Window | Qt::Tool);

if(!mHidden)
note->show();
}
}

QDialog* Notes::configureDialog()
{
NotesConfiguration *configDialog = new NotesConfiguration(settings());
configDialog->setAttribute(Qt::WA_DeleteOnClose, true);
return configDialog;
}
91 changes: 91 additions & 0 deletions plugin-notes/notes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/* BEGIN_COMMON_COPYRIGHT_HEADER
* (c)LGPL2+
*
* LXQt - a lightweight, Qt based, desktop toolset
* https://lxqt.org
*
* Copyright: 2012 Razor team
* Authors:
* Matteo Fois <[email protected]>
*
* This program or library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* END_COMMON_COPYRIGHT_HEADER */

#ifndef LXQT_NOTES_H
#define LXQT_NOTES_H

#include "../panel/ilxqtpanelplugin.h"
#include <QApplication>
#include <QMainWindow>
#include <QDesktopWidget>
#include <QFontMetrics>
#include <QTextEdit>
#include <QToolButton>
#include <QList>
#include <XdgIcon>

class StickyNote;

class Notes : public QObject, public ILXQtPanelPlugin
{
Q_OBJECT
public:
explicit Notes(const ILXQtPanelPluginStartupInfo &startupInfo);
~Notes();

virtual QWidget *widget() { return &mButton; }
virtual QString themeId() const { return "Notes"; }
virtual ILXQtPanelPlugin::Flags flags() const { return HaveConfigDialog; }

bool isSeparate() const { return true; }
QDialog *configureDialog();

public slots:
void realign();
void toggleShowHide();
void addNewNote();
void deleteNote(const qint64 &id);

protected slots:
void settingsChanged();

private:
QToolButton mButton;
bool mHidden;
QMap<qint64, StickyNote*> mNotes;
QString dataDir(); // (cannot be static for some reason)

void setIconsColor(const QString &color);
void setIconColor(const QString &icon, const QString &color);

QMainWindow window;
};


class NotesLibrary: public QObject, public ILXQtPanelPluginLibrary
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "lxqt.org/Panel/PluginInterface/3.0")
Q_INTERFACES(ILXQtPanelPluginLibrary)
public:
ILXQtPanelPlugin *instance(const ILXQtPanelPluginStartupInfo &startupInfo) const
{
return new Notes(startupInfo);
}
};

#endif
Loading