]> Shamusworld >> Repos - ttedit/blob - src/toolwindow.cpp
Removed unnecessary graphic files, updated tool palette.
[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         img = QImage(":/res/toolpal1.png");
34
35         // Set up sizes
36
37         sizeTPBM.rx() = img.width(), sizeTPBM.ry() = img.height();
38         sizeStamp.rx() = sizeTPBM.x() / 4, sizeStamp.ry() = sizeTPBM.y() / 3;
39
40         setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
41         setVisible(false);
42 }
43
44 QSize ToolWindow::sizeHint() const
45 {
46         return QSize(sizeTPBM.x(), sizeTPBM.y());
47 }
48
49 void ToolWindow::paintEvent(QPaintEvent * event)
50 {
51         QPainter p(this);
52         p.drawImage(QPoint(0, 0), img);
53
54         if (prevTool != -1)
55         {
56             //need ul corner of bitmap, ul corner of dest, width/height
57                 p.setCompositionMode(QPainter::RasterOp_NotSource);
58 //              QPoint pt(sizeStamp.x() * (prevTool & 0x03), sizeStamp.y() * (prevTool >> 2));
59                 QPoint pt(sizeStamp.x() * (prevTool % 4), sizeStamp.y() * (prevTool / 4));
60                 p.drawImage(pt.x(), pt.y(), img, pt.x(), pt.y(), sizeStamp.x(), sizeStamp.y());
61         }
62 }
63
64 //
65 // Find which tool we're pointing at
66 //
67 ToolType ToolWindow::FindSelectedTool(void)
68 {
69         // Get mouse coords relative to the tool palette window
70 //      wxPoint pt = ScreenToClient(wxGetMousePosition());
71         QPoint pt = mapFromGlobal(QCursor::pos());
72 //printf("pt = %u, %u\n", pt.x(), pt.y());
73
74         // Divide mouse coords by the bitmap stamp size to find which one is pointed to
75         uint32 x = (uint32)pt.x() / sizeStamp.x(), y = (uint32)pt.y() / sizeStamp.y();
76
77         // Preset failure into newTool, in case no new tool is selected
78         ToolType newTool = TOOLNone;
79
80         // NOTE: This works because x and y are UNSIGNED
81         if (x < 4 && y < 3)
82                 newTool = (ToolType)((y * 4) + x);
83
84         // We don't have 11 yet, so fix this if the user selected the blank space
85         if (newTool > 9)
86                 newTool = TOOLNone;
87
88         return newTool;
89 }