aboutsummaryrefslogtreecommitdiff
path: root/lumina-desktop/desktop-plugins/LDPluginContainer.h
blob: 14b87585404002ca7324ef4e911bb775dfb37409 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//===========================================
//  Lumina-DE source code
//  Copyright (c) 2014, Ken Moore
//  Available under the 3-clause BSD license
//  See the LICENSE file for full details
//===========================================
//  This class is the generic container for a desktop plugin that handles
//    saving/restoring all the movement and sizing
//===========================================
#ifndef _LUMINA_DESKTOP_DESKTOP_PLUGIN_CONTAINER_H
#define _LUMINA_DESKTOP_DESKTOP_PLUGIN_CONTAINER_H

#include <QObject>
#include <QMdiSubWindow>
#include <QSettings>
#include <QMoveEvent>
#include <QResizeEvent>
#include <QCloseEvent>
#include <QString>
#include <QFile>
#include <QIcon>
#include <QTimer>

#include "LDPlugin.h"

class LDPluginContainer : public QMdiSubWindow{
	Q_OBJECT
	
private:
	QSettings *settings;
	QTimer *syncTimer;
	bool locked, setup;
	
private slots:
	void saveGeometry(){
	    if(settings==0){ return; }
	    settings->setValue("location/x", this->pos().x());
	    settings->setValue("location/y", this->pos().y());
	    settings->setValue("location/width", this->size().width());
	    settings->setValue("location/height", this->size().height());
	    settings->sync();
	}
	
public:
	LDPluginContainer(LDPlugin *plugin = 0, bool islocked = true) : QMdiSubWindow(){
	  locked = islocked;
	  setup=true;
	  syncTimer = new QTimer(this);
	    syncTimer->setInterval(500); //save settings 1 second after it is moved
	    syncTimer->setSingleShot(true); //no repeats
	    connect(syncTimer, SIGNAL(timeout()), this, SLOT(saveGeometry()) );
	  this->setWhatsThis(plugin->ID());
	  if(locked){ this->setWindowFlags(Qt::FramelessWindowHint); }
	  else{ this->setWindowFlags(Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint); }
	  settings = plugin->settings; //save this pointer for access later
	  if(settings!=0){
	   if(settings->allKeys().isEmpty()){
	    //Brand new plugin - no location/size info saved yet
	    //save the initial size of the plugin - the initial location will be set automatically
	      QSize sz = plugin->sizeHint();
	      if(!sz.isValid()){ sz = QSize(64,64); }
	      settings->setValue("location/width", sz.width());
	      settings->setValue("location/height", sz.height());
	      settings->sync();
	   }
          }
	  this->setContentsMargins(0,0,0,0);
	  if(!locked){
	    this->setWindowTitle( plugin->ID().replace("---"," - ") );
	    this->setWidget( new QWidget() );
	    this->setWidget( plugin );
	    this->setWindowIcon(QIcon()); //remove the Qt icon
	  }else{
	    this->setStyleSheet("LDPluginContainer{ background: transparent; border: none;}");
	    this->setWidget(plugin);
	  }
	}
	
	~LDPluginContainer(){
	}

	void loadInitialPosition(){
	  QRect set(settings->value("location/x",-12345).toInt(), settings->value("location/y",-12345).toInt(), settings->value("location/width",this->widget()->sizeHint().width()).toInt(), settings->value("location/height",this->widget()->sizeHint().height()).toInt());
	  //qDebug() << "Initial Plugin Location:" << set.x() << set.y() << set.width() << set.height();
	    if(set.height() < 10){ set.setHeight(10); } //to prevent foot-shooting
	    if(set.width() < 10){ set.setWidth(10); } //to prevent foot-shooting
	    if(set.x()!=-12345 && set.y()!=-12345){
	      //custom location specified
	      this->setGeometry(set);
	    }else{
	      this->resize(set.width(), set.height());
	    }
	  setup=false; //done with setup
	}

signals:
	void PluginRemoved(QString);
	
protected:
	void moveEvent(QMoveEvent *event){
	  //Save this location to the settings
	  if(!locked && !setup){
	    if(syncTimer->isActive()){ syncTimer->stop(); }
	    syncTimer->start();
	    //qDebug() << "DP Move:" << event->pos().x() << event->pos().y();
	  }
	  QMdiSubWindow::moveEvent(event); //be sure to pass this event along to the container
	}
	
	void resizeEvent(QResizeEvent *event){
	  //Save this size info to the settings
	  if(!locked && !setup){
	    //qDebug() << "DP Resize:" << event->size().width() << event->size().height();
	    if(syncTimer->isActive()){ syncTimer->stop(); }
	    syncTimer->start();
	  }
	  QMdiSubWindow::resizeEvent(event); //be sure to pass this event along to the container
	}
	
	void closeEvent(QCloseEvent *event){
	  //qDebug() << "Desktop Plugin Close Event:" << this->whatsThis();
	  if( !this->whatsThis().isEmpty() && !locked){
	    //Plugin removed by the user - delete the settings file
	    locked = true; //ensure that the save settings routines don't do anything during the close
	    if(syncTimer->isActive()){ syncTimer->stop(); } //prevent save routine from running in a moment
	    QFile::remove( settings->fileName() );
	    emit PluginRemoved( this->whatsThis() );
	  }
	  settings = 0; //ensure we don't touch the settings file after a close event
	  event->accept();
	}
	
};

#endif
bgstack15