Commit bffd5f7c authored by LEFEBVREJP email's avatar LEFEBVREJP email
Browse files

Merge branch 'widgets' into 'master'

Initial working version of NumberPadWidget.

See merge request !44
parents f095cc95 3efc6701
Pipeline #14011 passed with stages
in 7 minutes and 41 seconds
...@@ -23,6 +23,7 @@ TRIBITS_SUBPACKAGE(widgets) ...@@ -23,6 +23,7 @@ TRIBITS_SUBPACKAGE(widgets)
SET(HEADERS SET(HEADERS
tabwidget.hh tabwidget.hh
tableview.hh tableview.hh
numberpadwidget.hh
) )
# #
# Moc the header files # Moc the header files
...@@ -50,6 +51,7 @@ ENDIF() ...@@ -50,6 +51,7 @@ ENDIF()
SET(SOURCES SET(SOURCES
tabwidget.cc tabwidget.cc
tableview.cc tableview.cc
numberpadwidget.cc
) )
##---------------------------------------------------------------------------## ##---------------------------------------------------------------------------##
...@@ -59,6 +61,7 @@ SET(SOURCES ...@@ -59,6 +61,7 @@ SET(SOURCES
TRIBITS_ADD_LIBRARY( TRIBITS_ADD_LIBRARY(
radixwidgets radixwidgets
SOURCES ${SOURCES} ${MOC_FILES} ${RESOURCE_RESULT} SOURCES ${SOURCES} ${MOC_FILES} ${RESOURCE_RESULT}
NOINSTALLHEADERS ${HEADERS}
) )
INSTALL(FILES ${HEADERS} INSTALL(FILES ${HEADERS}
......
...@@ -8,6 +8,9 @@ IF(USE_QT4) ...@@ -8,6 +8,9 @@ IF(USE_QT4)
QT4_WRAP_CPP(QMOC_FILES2 QT4_WRAP_CPP(QMOC_FILES2
radixtablewidget.hh radixtablewidget.hh
) )
QT4_WRAP_CPP(QMOC_FILES3
radixnumberpadwidget.hh
)
ELSE() ELSE()
QT5_WRAP_CPP(QMOC_FILES1 QT5_WRAP_CPP(QMOC_FILES1
radixtabwidget.hh radixtabwidget.hh
...@@ -15,6 +18,9 @@ ELSE() ...@@ -15,6 +18,9 @@ ELSE()
QT5_WRAP_CPP(QMOC_FILES2 QT5_WRAP_CPP(QMOC_FILES2
radixtablewidget.hh radixtablewidget.hh
) )
QT5_WRAP_CPP(QMOC_FILES3
radixnumberpadwidget.hh
)
ENDIF() ENDIF()
TRIBITS_ADD_EXECUTABLE(radixtabwidget TRIBITS_ADD_EXECUTABLE(radixtabwidget
NOEXEPREFIX NOEXEPREFIX
...@@ -26,3 +32,7 @@ TRIBITS_ADD_EXECUTABLE(radixtablewidget ...@@ -26,3 +32,7 @@ TRIBITS_ADD_EXECUTABLE(radixtablewidget
SOURCES radixtablewidget.cc ${QMOC_FILES2} SOURCES radixtablewidget.cc ${QMOC_FILES2}
) )
TRIBITS_ADD_EXECUTABLE(radixnumberpadwidget
NOEXEPREFIX
SOURCES radixnumberpadwidget.cc ${QMOC_FILES3}
)
/*
* @file: radixnumberpadwidget.cc
* @author: Jordan P. Lefebvre, lefebvrejp@ornl.gov
*/
#include "radixnumberpadwidget.hh"
#include <QApplication>
#include <QGridLayout>
#include <QHeaderView>
#include <QLabel>
#include <limits>
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
{
setGeometry(400, 250, 542, 390);
QWidget* container = new QWidget(this);
QGridLayout* layout = new QGridLayout(container);
QLineEdit* empty = new QLineEdit("Regular line edit:", this);
widget = new radix::NumberPadWidget(this);
layout->addWidget(empty);
layout->addWidget(widget);
setCentralWidget(container);
}
MainWindow::~MainWindow() {}
/******************************************************************************/
/******************************** MAIN PROGRAM ********************************/
/******************************************************************************/
int main(int argc, char** argv)
{
QApplication app(argc, argv);
MainWindow mainWindow;
mainWindow.show();
mainWindow.raise();
return app.exec();
}
#ifndef RADIX_RADIXWIDGETS_EXAMPLE_RADIXNUMBERPADWIDGET_HH_
#define RADIX_RADIXWIDGETS_EXAMPLE_RADIXNUMBERPADWIDGET_HH_
#include <QMainWindow>
#include "radixwidgets/numberpadwidget.hh"
namespace Ui
{
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
private:
radix::NumberPadWidget* widget;
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
};
#endif // RADIX_RADIXWIDGETS_EXAMPLE_RADIXNUMBERPADWIDGET_HH_
#include "radixwidgets/numberpadwidget.hh"
#include "radixbug/bug.hh"
#include <QApplication>
#include <QDesktopWidget>
#include <QGridLayout>
#include <QKeyEvent>
#include <QPoint>
#include <QPushButton>
#include <QRect>
#include <QSignalMapper>
#define NEXT_ROW_MARKER 0
namespace radix
{
struct KeyboardLayoutEntry
{
int key;
const char *label;
};
const KeyboardLayoutEntry keyboardLayout[] = {{Qt::Key_7, "7"},
{Qt::Key_8, "8"},
{Qt::Key_9, "9"},
{NEXT_ROW_MARKER, 0},
{Qt::Key_4, "4"},
{Qt::Key_5, "5"},
{Qt::Key_6, "6"},
{NEXT_ROW_MARKER, 0},
{Qt::Key_1, "1"},
{Qt::Key_2, "2"},
{Qt::Key_3, "3"},
{NEXT_ROW_MARKER, 0},
{Qt::Key_0, "0"},
{Qt::Key_Period, "."},
{Qt::Key_Backspace, "Backspace"}};
const int layoutSize = (sizeof(keyboardLayout) / sizeof(KeyboardLayoutEntry));
QString keyToCharacter(int key)
{
for (int i = 0; i < layoutSize; ++i)
{
if (keyboardLayout[i].key == key)
{
return QString::fromLatin1(keyboardLayout[i].label);
}
}
return QString();
}
NumberPadWidget::NumberPadWidget(QWidget *parent)
: QLineEdit(parent)
{
QSignalMapper *mapper = new QSignalMapper(this);
connect(mapper, SIGNAL(mapped(int)), SLOT(buttonClicked(int)));
// add widget containing
mNumberWidget = new QWidget(this);
QGridLayout *numLayout = new QGridLayout(mNumberWidget);
mNumberWidget->hide();
mNumberWidget->setWindowFlags(Qt::WindowDoesNotAcceptFocus | Qt::Tool |
Qt::FramelessWindowHint |
Qt::WindowStaysOnTopHint);
int row = 0;
int col = 0;
// Create keyboard widget
for (int i = 0; i < layoutSize; ++i)
{
if (keyboardLayout[i].key == NEXT_ROW_MARKER)
{
row++;
col = 0;
continue;
}
QPushButton *button = new QPushButton(this);
button->setObjectName("number_pad_button");
button->setText(QString::fromLatin1(keyboardLayout[i].label));
mapper->setMapping(button, keyboardLayout[i].key);
connect(button, SIGNAL(clicked()), mapper, SLOT(map()));
numLayout->addWidget(button, row, col);
col++;
}
}
void NumberPadWidget::buttonClicked(int key)
{
QLineEdit::keyPressEvent(new QKeyEvent(QEvent::KeyPress, key, Qt::NoModifier,
keyToCharacter(key)));
}
void NumberPadWidget::focusInEvent(QFocusEvent *event)
{
QLineEdit::focusInEvent(event);
//
// Determine where to place the widget
QRect desktopRect = QApplication::desktop()->availableGeometry(this);
QPoint center = desktopRect.center();
mNumberWidget->move(center.x() - mNumberWidget->width() * 0.5,
center.y() - mNumberWidget->height() * 0.5);
mNumberWidget->show();
}
void NumberPadWidget::focusOutEvent(QFocusEvent *event)
{
QLineEdit::focusOutEvent(event);
mNumberWidget->hide();
}
bool NumberPadWidget::numberPadVisible() const { return QWidget::isVisible(); }
} // namespace radix
#ifndef RADIX_RADIXWIDGETS_NUMBERPADWIDGET_HH_
#define RADIX_RADIXWIDGETS_NUMBERPADWIDGET_HH_
#include <QLineEdit>
#include <QWidget>
namespace radix
{
class NumberPadWidget : public QLineEdit
{
Q_OBJECT
QWidget* mNumberWidget;
public:
NumberPadWidget(QWidget* parent = 0);
public slots:
void focusInEvent(QFocusEvent*) override;
void focusOutEvent(QFocusEvent*) override;
bool numberPadVisible() const;
private slots:
void buttonClicked(int key);
}; // class
} // namespace radix
#endif /** RADIX_RADIXWIDGETS_NUMBERPADWIDGET_HH_ */
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