Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dockable and sizable bitfield widget #45

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/SourceFiles.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ core/evaluator.h
core/functions.h
core/manual.h
gui/aboutbox.h
gui/bitfielddock.h
gui/bitfieldwidget.h
gui/bookdock.h
gui/constantsdock.h
Expand Down Expand Up @@ -39,6 +40,7 @@ core/numberformatter.cpp
core/pageserver.cpp
core/settings.cpp
gui/aboutbox.cpp
gui/bitfielddock.cpp
gui/bitfieldwidget.cpp
gui/bookdock.cpp
gui/constantsdock.cpp
Expand Down
6 changes: 4 additions & 2 deletions src/core/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ void Settings::load()
userFunctionsDockVisible = settings->value(key + QLatin1String("UserFunctionsDockVisible"), false).toBool();
formulaBookDockVisible = settings->value(key + QLatin1String("FormulaBookDockVisible"), false).toBool();
constantsDockVisible = settings->value(key + QLatin1String("ConstantsDockVisible"), false).toBool();
bitFieldDockVisible = settings->value(key + QLatin1String("BitFieldDockVisible"), false).toBool();
bitFieldDockTitle = settings->value(key + QLatin1String("BitFieldDockTitle"), true).toBool();
windowAlwaysOnTop = settings->value(key + QLatin1String("WindowAlwaysOnTop"), false).toBool();
bitfieldVisible = settings->value(key + QLatin1String("BitfieldVisible"), false).toBool();

windowPosition = settings->value(key + QLatin1String("WindowPosition"), QPoint()).toPoint();
windowSize = settings->value(key + QLatin1String("WindowSize"), QSize(640, 480)).toSize();
Expand Down Expand Up @@ -263,12 +264,13 @@ void Settings::save()
settings->setValue(key + QLatin1String("StatusBarVisible"), statusBarVisible);
settings->setValue(key + QLatin1String("VariablesDockVisible"), variablesDockVisible);
settings->setValue(key + QLatin1String("UserFunctionsDockVisible"), userFunctionsDockVisible);
settings->setValue(key + QLatin1String("BitFieldDockVisible"), bitFieldDockVisible);
settings->setValue(key + QLatin1String("BitFieldDockTitle"), bitFieldDockTitle);
settings->setValue(key + QLatin1String("WindowPosition"), windowPosition);
settings->setValue(key + QLatin1String("WindowSize"), windowSize);
settings->setValue(key + QLatin1String("WindowAlwaysOnTop"), windowAlwaysOnTop);
settings->setValue(key + QLatin1String("State"), windowState);
settings->setValue(key + QLatin1String("Maximized"), maximized);
settings->setValue(key + QLatin1String("BitfieldVisible"), bitfieldVisible);

key = KEY + QLatin1String("/Display/");

Expand Down
3 changes: 2 additions & 1 deletion src/core/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ class Settings {
bool statusBarVisible;
bool variablesDockVisible;
bool userFunctionsDockVisible;
bool bitFieldDockVisible;
bool bitFieldDockTitle;
bool windowOnfullScreen;
bool bitfieldVisible;

int colorScheme;
QString displayFont;
Expand Down
59 changes: 59 additions & 0 deletions src/gui/bitfielddock.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// This file is part of the SpeedCrunch project
// Copyright (C) 2007 Ariya Hidayat <[email protected]>
// Copyright (C) 2007, 2008, 2009, 2010 Helder Correia <[email protected]>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING. If not, write to
// the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301, USA.

#include "math/hmath.h"

#include "gui/bitfielddock.h"
#include "gui/bitfieldwidget.h"

#include <QEvent>

BitFieldDock::BitFieldDock(QWidget *parent)
: QDockWidget(parent)
, m_widget(new BitFieldWidget(this))
{
setWidget( m_widget );
retranslateText();
}

void BitFieldDock::displayTitleBar(bool on)
{
if (on)
setTitleBarWidget(NULL);
else
setTitleBarWidget(new QWidget());
}

void BitFieldDock::updateBits(const HNumber& number)
{
m_widget->updateBits(number);
}

void BitFieldDock::retranslateText()
{
setWindowTitle(tr("Bitfield"));
}

void BitFieldDock::changeEvent(QEvent *e)
{
if (e->type() == QEvent::LanguageChange)
retranslateText();
else
QDockWidget::changeEvent(e);
}
49 changes: 49 additions & 0 deletions src/gui/bitfielddock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// This file is part of the SpeedCrunch project
// Copyright (C) 2007 Ariya Hidayat <[email protected]>
// Copyright (C) 2007, 2008, 2009, 2010 Helder Correia <[email protected]>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING. If not, write to
// the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301, USA.

#ifndef GUI_BITFIELDDOCK_H
#define GUI_BITFIELDDOCK_H

#include <QDockWidget>

class BitFieldWidget;
class HNumber;

class BitFieldDock : public QDockWidget
{
Q_OBJECT

public:
BitFieldDock(QWidget *parent = 0);

public slots:
void displayTitleBar(bool);
void updateBits(const HNumber&);

protected:
virtual void changeEvent(QEvent *);
void retranslateText();

private:
Q_DISABLE_COPY(BitFieldDock)

BitFieldWidget *m_widget;
};

#endif // GUI_BITFIELDDOCK_H
63 changes: 41 additions & 22 deletions src/gui/bitfieldwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,29 @@
#include <QListIterator>
#include <QPainter>
#include <QPaintEvent>
#include <QPushButton>
#include <QToolButton>

//------------------------------------------------------------------------------

BitWidget::BitWidget(int bitPosition, QWidget* parent)
: QLabel(parent),
m_state(false)
{
setFixedSize(SizePixels, SizePixels);
setMinimumSize(SizePixels, SizePixels);
setMaximumSize(SizePixels*4, SizePixels*4);
setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);

HNumber number(HMath::raise(HNumber(2), bitPosition));
setToolTip(QString("2<sup>%1</sup> = %2")
.arg(bitPosition)
.arg(HMath::format(number, 'd')));
}

QSize BitWidget::sizeHint() const
{
return QSize(SizePixels*4, SizePixels*4);
}

void BitWidget::mouseReleaseEvent(QMouseEvent*)
{
setState(!m_state);
Expand All @@ -58,6 +67,8 @@ void BitWidget::paintEvent(QPaintEvent* event)
painter.drawRect(event->rect());
}

//------------------------------------------------------------------------------

BitFieldWidget::BitFieldWidget(QWidget* parent) :
QWidget(parent)
{
Expand All @@ -71,6 +82,9 @@ BitFieldWidget::BitFieldWidget(QWidget* parent) :
QGridLayout* fieldLayout = new QGridLayout;
int bitOffset = 0;

// add empty row so that there will be the same row-spacing maring at top and bottom
fieldLayout->addWidget(new QWidget(), 0, 0, -1, -1);

for (int column = 0; column < 17; ++column) {
if ((column % 2) == 0) {
if ((column % 4) != 0)
Expand All @@ -90,8 +104,8 @@ BitFieldWidget::BitFieldWidget(QWidget* parent) :
topNumberLabel->setText(QString("%1").arg(topNumber));
bottomNumberLabel->setText(QString("%1").arg(bottomNumber));

fieldLayout->addWidget(topNumberLabel, 0, column);
fieldLayout->addWidget(bottomNumberLabel, 1, column);
fieldLayout->addWidget(topNumberLabel, 1, column);
fieldLayout->addWidget(bottomNumberLabel, 2, column);

} else {
QHBoxLayout* bottomLayout(new QHBoxLayout);
Expand All @@ -105,40 +119,45 @@ BitFieldWidget::BitFieldWidget(QWidget* parent) :

++bitOffset;

fieldLayout->addLayout(bottomLayout, 1, column, Qt::AlignCenter);
fieldLayout->addLayout(topLayout, 0, column, Qt::AlignCenter);
fieldLayout->addLayout(bottomLayout, 2, column, Qt::AlignCenter);
fieldLayout->addLayout(topLayout, 1, column, Qt::AlignCenter);
}
}

QPushButton* resetButton = new QPushButton("0");
resetButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
QToolButton* resetButton = new QToolButton();
resetButton->setText("0");
resetButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
connect(resetButton, SIGNAL(clicked()), this, SLOT(resetBits()));

QPushButton* invertButton = new QPushButton("~");
invertButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
QToolButton* invertButton = new QToolButton();
invertButton->setText("~");
invertButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
connect(invertButton, SIGNAL(clicked()), this, SLOT(invertBits()));

QPushButton* shiftLeftButton = new QPushButton("<<");
shiftLeftButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
QToolButton* shiftLeftButton = new QToolButton();
shiftLeftButton->setText("<<");
shiftLeftButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
connect(shiftLeftButton, SIGNAL(clicked()), this, SLOT(shiftBitsLeft()));

QPushButton* shiftRightButton = new QPushButton(">>");
shiftRightButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
QToolButton* shiftRightButton = new QToolButton();
shiftRightButton->setText(">>");
shiftRightButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
connect(shiftRightButton, SIGNAL(clicked()), this, SLOT(shiftBitsRight()));

QVBoxLayout* buttonsLayout = new QVBoxLayout;
buttonsLayout->addWidget(resetButton);
buttonsLayout->addWidget(shiftLeftButton);
int col = fieldLayout->columnCount();
fieldLayout->addWidget(resetButton, 1, col);
fieldLayout->addWidget(shiftLeftButton, 2, col);
fieldLayout->addWidget(invertButton, 1, ++col);
fieldLayout->addWidget(shiftRightButton, 2, col);

QVBoxLayout* buttonsLayout2 = new QVBoxLayout;
buttonsLayout2->addWidget(invertButton);
buttonsLayout2->addWidget(shiftRightButton);
// add empty row eating as much as possible (no spacing between format rows)
fieldLayout->addWidget(new QWidget(), 3, 0, -1, -1);
fieldLayout->setRowStretch(3, 1);

QHBoxLayout* mainLayout = new QHBoxLayout(this);
mainLayout->setContentsMargins(2, 0, 2, 0);
mainLayout->addStretch();
mainLayout->addLayout(fieldLayout);
mainLayout->addLayout(buttonsLayout);
mainLayout->addLayout(buttonsLayout2);
mainLayout->addStretch();
}

Expand Down
7 changes: 6 additions & 1 deletion src/gui/bitfieldwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@

class HNumber;

//------------------------------------------------------------------------------

class BitWidget : public QLabel {
Q_OBJECT

public:
explicit BitWidget(int apos, QWidget* parent = 0);
virtual QSize sizeHint() const;

bool state() const { return m_state; }
void setState(bool state) { m_state = state; update(); }
Expand All @@ -43,14 +46,16 @@ class BitWidget : public QLabel {

private:
enum {
SizePixels = 20,
SizePixels = 5,
};

Q_DISABLE_COPY(BitWidget)

bool m_state;
};

//------------------------------------------------------------------------------

class BitFieldWidget : public QWidget {
Q_OBJECT

Expand Down
5 changes: 5 additions & 0 deletions src/gui/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,11 @@ void Editor::keyPressEvent(QKeyEvent* event)
QPlainTextEdit::keyPressEvent(event);
}

void Editor::moveEvent(QMoveEvent* event)
{
emit posChanged(event);
}

void Editor::wheelEvent(QWheelEvent* event)
{
if (event->delta() > 0)
Expand Down
3 changes: 3 additions & 0 deletions src/gui/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class SyntaxHighlighter;
class QEvent;
class QKeyEvent;
class QMimeData;
class QMoveEvent;
class QTimeLine;
class QTreeWidget;
class QWheelEvent;
Expand Down Expand Up @@ -71,6 +72,7 @@ class Editor : public QPlainTextEdit {
void copySequencePressed();
void pageDownPressed();
void pageUpPressed();
void posChanged(const QMoveEvent* event);
void returnPressed();
void shiftDownPressed();
void shiftUpPressed();
Expand Down Expand Up @@ -109,6 +111,7 @@ protected slots:
virtual void changeEvent(QEvent*);
virtual void focusOutEvent(QFocusEvent*);
virtual void keyPressEvent(QKeyEvent*);
virtual void moveEvent(QMoveEvent*);
virtual void paintEvent(QPaintEvent*);
virtual void wheelEvent(QWheelEvent*);

Expand Down
Loading