summaryrefslogtreecommitdiff
path: root/plasma/gtft.cpp
blob: 7ae993a85b764d68a26c12c2b6490a2891d2a5fc (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216

#include <KConfigDialog>
#include <KSharedConfig>
#include <KServiceTypeTrader>
#include <kglobalsettings.h>

#include <gtft.h>
#include <configdialog.h>

GtftApplet::GtftApplet(QObject* parent, const QVariantList& args)
   : Plasma::Applet(parent, args)
{
   configDialog = 0;
   vdrWidth = 720;
   vdrHeight = 576;

   thread = new ComThread();

   setHasConfigurationInterface(true);
   setCacheMode(QGraphicsItem::NoCache);
   setBackgroundHints(Plasma::Applet::NoBackground);
   setAspectRatioMode(Plasma::KeepAspectRatio);  // Plasma::IgnoreAspectRatio

   resize(400, 300);
}

GtftApplet::~GtftApplet()
{
   if (thread)
   {
      tell(eloAlways, "Stopping thread");

      thread->stop();

      if (!thread->wait(1000))
         tell(eloAlways, "Thread would not end!");
      else
         tell(eloAlways, "Thread ended regularly");

      delete thread;
   }
}

void GtftApplet::init()
{
   // picture.load("/home/wendel/test.jpg");

   // read config
   
   KConfigGroup cg = config();
   
   smoothScaling = cg.readEntry("smoothScaling", true);
   host = cg.readEntry("vdr host", "localhost");
   port = cg.readEntry("vdr port", 2039);

   connect(thread, SIGNAL(updateImage(unsigned char*, int)),
           this, SLOT(updateImage(unsigned char*, int)));

   tell(eloAlways, "Starting thread");

   thread->setHost(host.toAscii());
   thread->setPort(port);

   thread->start();
}

//***************************************************************************
// Configuration
//***************************************************************************

void GtftApplet::createConfigurationInterface(KConfigDialog* parent)
{
   configDialog = new ConfigDialog(parent);

   parent->addPage(configDialog, i18n("General"), "configure");
   parent->setDefaultButton(KDialog::Ok);
   parent->showButtonSeparator(true);

   connect(parent, SIGNAL(applyClicked()), this, SLOT(configAccepted()));
   connect(parent, SIGNAL(okClicked()), this, SLOT(configAccepted()));

   configDialog->setSmoothScaling(smoothScaling);
   configDialog->setHost(host);
   configDialog->setPort(port);
}

void GtftApplet::configAccepted()
{
    KConfigGroup cg = config();

    // connection parameter changed ?

    if (host != configDialog->host() ||
        port != configDialog->port())
    {      
       // reconnect ..

       thread->stop();
       host = configDialog->host();
       port = configDialog->port();
       thread->setHost(host.toAscii());
       thread->setPort(port);
       thread->start();
    }

    smoothScaling = configDialog->smoothScaling();

    cg.writeEntry("smoothScaling", smoothScaling);
    cg.writeEntry("vdr host", host);
    cg.writeEntry("vdr port", port);

    emit configNeedsSaving();
}

void GtftApplet::updateImage(unsigned char* buffer, int size)
{
   thread->getBufferLock()->lockForRead();
   picture.loadFromData(buffer, size);
   thread->getBufferLock()->unlock();

   update();
}

void GtftApplet::paintInterface(QPainter* p, const QStyleOptionGraphicsItem* option,
                                const QRect& rect)
{
   Q_UNUSED(option);
   QPixmap* pixmap;

   Qt::TransformationMode tm = smoothScaling ? Qt::SmoothTransformation : Qt::FastTransformation;

   if (!picture.isNull())
      pixmap = &picture;
   else
      return ;

   QPixmap scaledImage = pixmap->scaled(rect.size(), Qt::KeepAspectRatio, tm);
   p->drawPixmap(rect, scaledImage);

   // notice some data for 'scaling' the mouse cooridinates ...

   paintRect = rect;
   vdrWidth = pixmap->width();
   vdrHeight = pixmap->height();
}

//***************************************************************************
// User Action
//***************************************************************************

void GtftApplet::keyPressEvent(QKeyEvent* keyEvent)
{
   thread->mouseEvent(0, 0, keyEvent->nativeScanCode(), ComThread::efKeyboard);
}

void GtftApplet::mouseMoveEvent(QGraphicsSceneMouseEvent* /*event*/)
{
   // don't drag the widget ...
}

void GtftApplet::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event)
{
   QPoint p = clickCoordinate(event->screenPos());

   thread->mouseEvent(p.x(), p.y(), cGraphTftComService::mbLeft, 
                      cGraphTftComService::efDoubleClick);
}

void GtftApplet::mouseReleaseEvent(QGraphicsSceneMouseEvent* /*event*/)
{
   // #TODO
   // Mouse gestures
}

void GtftApplet::wheelEvent(QGraphicsSceneWheelEvent* wheelEvent)
{
   QPoint p = clickCoordinate(wheelEvent->screenPos());

   if (wheelEvent->delta() > 0)
      thread->mouseEvent(p.x(), p.y(), cGraphTftComService::mbWheelUp, 0);
   else
      thread->mouseEvent(p.x(), p.y(), cGraphTftComService::mbWheelDown, 0);
}

void GtftApplet::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
   QPoint p = clickCoordinate(event->screenPos());

   tell(4, "mousePressEvent %d/%d", p.x(), p.y());

   if (event->buttons() == Qt::LeftButton) 
      thread->mouseEvent(p.x(), p.y(), cGraphTftComService::mbLeft, 0);
   else if (event->buttons() == Qt::MidButton)
      thread->mouseEvent(p.x(), p.y(), cGraphTftComService::mbRight, 0);
}

QPoint GtftApplet::clickCoordinate(QPoint screenPos)
{
   QPoint p;

   // calc position relative to the draw area (the image)
   
   p.setX(screenPos.x() - (geometry().x() + paintRect.x()));
   p.setY(screenPos.y() - (geometry().y() + paintRect.y()));

   // scale

   p.setX((int)(((double)p.x() / (double)paintRect.width()) * (double)vdrWidth));
   p.setY((int)(((double)p.y() / (double)paintRect.height()) * (double)vdrHeight));

   return p;
}

//***************************************************************************

#include "gtft.moc"