]> Shamusworld >> Repos - ttedit/blob - src/toolwindow.cpp
Converted project from wxWidgets to Qt. This will be the LAST time this
[ttedit] / src / toolwindow.cpp
1 //
2 // TOOLWINDOW.CPP - Tool Palette window
3 // by James L. Hammons
4 // (C) 2008 Underground Software
5 //
6 // JLH = James L. Hammons <jlhamm@acm.org>
7 //
8 // Who  When        What
9 // ---  ----------  -------------------------------------------------------------
10 // JLH  08/28/2008  Created this file
11 // JLH  03/11/2009  Converted from wxWidgets to Qt
12 //
13
14 // FIXED:
15 //
16 // STILL TO BE DONE:
17 //
18 //
19
20 // Uncomment this for debugging...
21 #define DEBUG
22 #define DEBUGFOO            // Various tool debugging...
23 #define DEBUGTP                         // Toolpalette debugging...
24
25 #include "toolwindow.h"
26 #include "types.h"
27
28
29 ToolWindow::ToolWindow(void): QWidget(NULL, Qt::Window | Qt::FramelessWindowHint),
30         prevTool(TOOLNone)
31 {
32         img = QImage(":/res/toolpal1.xpm");
33
34         // Set up sizes
35
36         sizeTPBM.rx() = img.width(), sizeTPBM.ry() = img.height();
37         sizeStamp.rx() = sizeTPBM.x() / 4, sizeStamp.ry() = sizeTPBM.y() / 2;
38
39         setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
40         setVisible(false);
41 }
42
43 QSize ToolWindow::sizeHint() const
44 {
45         return QSize(sizeTPBM.x(), sizeTPBM.y());
46 }
47
48 void ToolWindow::paintEvent(QPaintEvent * event)
49 {
50         QPainter p(this);
51         p.drawImage(QPoint(0, 0), img);
52
53         if (prevTool != -1)
54         {
55             //need ul corner of bitmap, ul corner of dest, width/height
56                 p.setCompositionMode(QPainter::RasterOp_NotSource);
57                 QPoint pt(sizeStamp.x() * (prevTool & 0x03), sizeStamp.y() * (prevTool >> 2));
58                 p.drawImage(pt.x(), pt.y(), img, pt.x(), pt.y(), sizeStamp.x(), sizeStamp.y());
59         }
60 }
61
62 //
63 // Find which tool we're pointing at
64 //
65 ToolType ToolWindow::FindSelectedTool(void)
66 {
67         // Get mouse coords relative to the tool palette window
68 //      wxPoint pt = ScreenToClient(wxGetMousePosition());
69         QPoint pt = mapFromGlobal(QCursor::pos());
70 //printf("pt = %u, %u\n", pt.x(), pt.y());
71
72         // Divide mouse coords by the bitmap stamp size to find which one is pointed to
73         uint32 x = (uint32)pt.x() / sizeStamp.x(), y = (uint32)pt.y() / sizeStamp.y();
74
75         // Preset failure into newTool, in case no new tool is selected
76         ToolType newTool = TOOLNone;
77
78         // NOTE: This works because x and y are UNSIGNED
79         if (x < 4 && y < 2)
80                 newTool = (ToolType)((y * 4) + x);
81
82         return newTool;
83 }