Commit a2eb4531 authored by Janis Born's avatar Janis Born

initial commit

parents
Pipeline #1367 skipped
include (plugin)
openflipper_plugin (INSTALLDATA Icons)
#include "DidYouKnowPlugin.hh"
DidYouKnowPlugin::DidYouKnowPlugin()
{
}
DidYouKnowPlugin::~DidYouKnowPlugin()
{
}
void DidYouKnowPlugin::initializePlugin()
{
}
void DidYouKnowPlugin::pluginsInitialized() {
mDidYouKnowWidget.show();
mDidYouKnowWidget.raise();
mDidYouKnowWidget.activateWindow();
}
#if QT_VERSION < 0x050000
Q_EXPORT_PLUGIN2(didyouknowplugin, DidYouKnowPlugin);
#endif
#ifndef DIDYOUKNOWPLUGIN_HH
#define DIDYOUKNOWPLUGIN_HH
#include <OpenFlipper/BasePlugin/BaseInterface.hh>
#include "DidYouKnowWidget.hh"
class DidYouKnowPlugin : public QObject, BaseInterface
{
Q_OBJECT
Q_INTERFACES(BaseInterface)
#if QT_VERSION >= 0x050000
Q_PLUGIN_METADATA(IID "org.OpenFlipper.Plugins.Plugin-DidYouKnow")
#endif
signals:
public:
DidYouKnowPlugin();
~DidYouKnowPlugin();
// BaseInterface
QString name() { return QString("Did You Know?"); }
QString description() { return QString("Displays a Welcome screen with helpful tips and hints about OpenFlipper and geometry processing in general."); }
private slots:
// BaseInterface
void initializePlugin();
void pluginsInitialized();
public slots:
QString version() { return QString("1.0"); }
private:
DidYouKnowWidget mDidYouKnowWidget;
};
#endif //DIDYOUKNOWPLUGIN_HH
#include "DidYouKnowWidget.hh"
#include <OpenFlipper/common/GlobalOptions.hh>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>
#include <QStandardPaths>
#include <QDateTime>
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QEventLoop>
#include <iostream>
DidYouKnowWidget::DidYouKnowWidget(QWidget* _parent)
{
setWindowTitle("Tip of the Day");
setWindowFlags(Qt::Dialog | Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
QSize labelSize(420, 200);
mLabelTip.setText("Did you know...");
mLabelTip.setMinimumSize(labelSize);
mLabelTip.setMaximumSize(labelSize);
mLabelTip.setAlignment(Qt::AlignTop);
mLabelTip.setWordWrap(true);
mCheckBoxShowTipsOnStartup.setText("&Show tips at startup");
mCheckBoxShowTipsOnStartup.setChecked(true);
mPushButtonNextTip.setText("&Next Tip");
connect(&mPushButtonNextTip, SIGNAL(clicked(bool)), this, SLOT(displayNextTip()));
mPushButtonClose.setText("&Close");
connect(&mPushButtonClose, SIGNAL(clicked(bool)), this, SLOT(close()));
mHBoxLayoutBottomRow.addWidget(&mCheckBoxShowTipsOnStartup);
mHBoxLayoutBottomRow.addWidget(&mPushButtonNextTip);
mHBoxLayoutBottomRow.addWidget(&mPushButtonClose);
mVBoxLayoutGlobal.addWidget(&mLabelTip);
mVBoxLayoutGlobal.addLayout(&mHBoxLayoutBottomRow);
setLayout(&mVBoxLayoutGlobal);
setFixedSize(sizeHint());
try {
updateTips();
displayRandomTip();
} catch (ConfigError& e) {
std::cout << "Could not display tip of the day: " << e.what() << std::endl;
}
}
QString DidYouKnowWidget::libraryFileName() const {
QString configPath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
QString path = QDir(configPath).filePath("didYouKnowLibrary.json");
return path;
}
void DidYouKnowWidget::updateTips()
{
// Number of seconds after which the local tips library is considered outdated
const int outdatedSeconds = 1 * 24 * 60 * 60;
const QUrl libraryURL("https://graphics.rwth-aachen.de:9000/Staff/TipOfTheDayLibrary/raw/master/tips.json");
// Check for existing local library file
QFileInfo libraryFileInfo(libraryFileName());
bool downloadNewLibrary = false;
if (!libraryFileInfo.exists()) {
std::cout << "No local tips library file found." << std::endl;
downloadNewLibrary = true;
}
else if (libraryFileInfo.lastModified().secsTo(QDateTime::currentDateTime()) > outdatedSeconds) {
std::cout << "Local tips library file is outdated." << std::endl;
downloadNewLibrary = true;
}
if (downloadNewLibrary) {
std::cout << "Downloading tips library from " << libraryURL.toString().toStdString() << " ..." << std::endl;
QNetworkAccessManager manager;
QNetworkRequest request(libraryURL);
QNetworkReply* reply = manager.get(request);
QEventLoop loop;
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
if (reply->error() == QNetworkReply::NoError) {
QByteArray body = reply->readAll();
QFile libraryFile(libraryFileName());
if (!libraryFile.open(QIODevice::WriteOnly)) {
throw ConfigError("Could not open " + libraryFile.fileName().toStdString());
}
libraryFile.write(body);
}
}
mTips.clear();
QFile libraryFile(libraryFileName());
if (!libraryFile.open(QIODevice::ReadOnly)) {
throw ConfigError("Could not open " + libraryFile.fileName().toStdString());
}
QByteArray libraryData = libraryFile.readAll();
QJsonDocument libraryDoc = QJsonDocument::fromJson(libraryData);
if (!libraryDoc.isObject()) {
throw ConfigError("Could not load JSON data");
}
QJsonObject libraryObj = libraryDoc.object();
if (!libraryObj["tips"].isArray()) {
throw ConfigError("Could not find JSON array named 'tips' on top level");
}
QJsonArray tips = libraryObj["tips"].toArray();
for (QJsonArray::Iterator it = tips.begin(); it != tips.end(); ++it) {
if (!(*it).isString()) {
throw ConfigError("Element of 'tips' array is not a string");
}
mTips.push_back((*it).toString());
}
}
void DidYouKnowWidget::displayTip(std::size_t _tipIndex)
{
if (_tipIndex < mTips.size()) {
QString text;
text += "<h1><img src=\"" + OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator() + "lightbulb.png\"> Did you know...</h1>";
text += "<hr>";
text += mTips[_tipIndex];
mLabelTip.setText(text);
}
}
void DidYouKnowWidget::displayRandomTip()
{
mCurrentTipIndex = rand() % mTips.size();
displayTip(mCurrentTipIndex);
}
void DidYouKnowWidget::displayNextTip()
{
mCurrentTipIndex = (mCurrentTipIndex + 1) % mTips.size();
displayTip(mCurrentTipIndex);
}
#ifndef DIDYOUKNOWWIDGET_HH
#define DIDYOUKNOWWIDGET_HH
#include <QDialog>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QCheckBox>
#include <QLabel>
#include <stdexcept>
class DidYouKnowWidget : public QDialog
{
Q_OBJECT
public:
explicit DidYouKnowWidget(QWidget* _parent = 0);
private:
struct ConfigError : public std::runtime_error
{
ConfigError(const std::string& _what) :
std::runtime_error(_what)
{
}
};
QString libraryFileName() const;
private slots:
void updateTips();
void displayTip(std::size_t _tipIndex);
void displayRandomTip();
void displayNextTip();
private:
std::vector<QString> mTips;
std::size_t mCurrentTipIndex;
QVBoxLayout mVBoxLayoutGlobal;
QHBoxLayout mHBoxLayoutBottomRow;
QLabel mLabelTip;
QPushButton mPushButtonNextTip;
QPushButton mPushButtonClose;
QCheckBox mCheckBoxShowTipsOnStartup;
};
#endif //DIDYOUKNOWWIDGET_HH
{"tips":[
"Filling a non-regular hole of valence <i>n</i> in a cubic manifold B-spline with <i>n</i> bicubic Bezier patches with <i>G<sup>1</sup></i> continuity is impossible in general if <i>n</i> is even.",
"A closed, regular quad mesh is a torus."
]}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment