Skip to content

Instantly share code, notes, and snippets.

@carlosdelfino
Last active August 27, 2021 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlosdelfino/a13f5d706dc8119bc07fac4c425799ba to your computer and use it in GitHub Desktop.
Save carlosdelfino/a13f5d706dc8119bc07fac4c425799ba to your computer and use it in GitHub Desktop.
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "memorytable.h"
#include <QScrollBar>
#include <QHeaderView>
MemoryTable::MemoryTable(QWidget *parent): QTableView(parent){
fieldNameTableView = new QTableView(this);
}
MemoryTable::MemoryTable(QAbstractItemModel * model)
{
fieldNameTableView = new QTableView(this);
init(model);
}
MemoryTable::~MemoryTable()
{
delete fieldNameTableView;
}
void MemoryTable::init(QAbstractItemModel *model){
setModel(model);
init();
//connect the headers and scrollbars of both tableviews together
connect(horizontalHeader(),&QHeaderView::sectionResized, this,
&MemoryTable::updateSectionWidth);
connect(verticalHeader(),&QHeaderView::sectionResized, this,
&MemoryTable::updateSectionHeight);
connect(fieldNameTableView->verticalScrollBar(), &QAbstractSlider::valueChanged,
verticalScrollBar(), &QAbstractSlider::setValue);
connect(verticalScrollBar(), &QAbstractSlider::valueChanged,
fieldNameTableView->verticalScrollBar(), &QAbstractSlider::setValue);
}
void MemoryTable::init()
{
fieldNameTableView->setModel(model());
fieldNameTableView->setFocusPolicy(Qt::NoFocus);
fieldNameTableView->verticalHeader()->hide();
fieldNameTableView->horizontalHeader()
->setSectionResizeMode(QHeaderView::Fixed);
viewport()->stackUnder(fieldNameTableView);
fieldNameTableView->setStyleSheet(
"QTableView { border: none;"
"background-color: #8EDE21;"
"selection-background-color: #999}"); //for demo purposes
fieldNameTableView->setSelectionModel(selectionModel());
for (int col = 1; col < model()->columnCount(); ++col)
fieldNameTableView->setColumnHidden(col, true);
fieldNameTableView->setColumnWidth(0, columnWidth(0) );
fieldNameTableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
fieldNameTableView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
fieldNameTableView->show();
updateRegisterNamesTableGeometry();
setHorizontalScrollMode(ScrollPerPixel);
setVerticalScrollMode(ScrollPerPixel);
fieldNameTableView->setVerticalScrollMode(ScrollPerPixel);
}
void MemoryTable::updateSectionWidth(int logicalIndex, int /* oldSize */, int newSize)
{
if (logicalIndex == 0){
fieldNameTableView->setColumnWidth(0, newSize);
updateRegisterNamesTableGeometry();
}
}
void MemoryTable::updateSectionHeight(int logicalIndex, int /* oldSize */, int newSize)
{
fieldNameTableView->setRowHeight(logicalIndex, newSize);
}
void MemoryTable::resizeEvent(QResizeEvent * event)
{
QTableView::resizeEvent(event);
updateRegisterNamesTableGeometry();
}
QModelIndex MemoryTable::moveCursor(CursorAction cursorAction,
Qt::KeyboardModifiers modifiers)
{
QModelIndex current = QTableView::moveCursor(cursorAction, modifiers);
if (cursorAction == MoveLeft && current.column() > 0
&& visualRect(current).topLeft().x() < fieldNameTableView->columnWidth(0) ){
const int newValue = horizontalScrollBar()->value() + visualRect(current).topLeft().x()
- fieldNameTableView->columnWidth(0);
horizontalScrollBar()->setValue(newValue);
}
return current;
}
void MemoryTable::scrollTo (const QModelIndex & index, ScrollHint hint){
if (index.column() > 0)
QTableView::scrollTo(index, hint);
}
void MemoryTable::updateRegisterNamesTableGeometry()
{
fieldNameTableView->setGeometry(
verticalHeader()->width() + frameWidth(),
frameWidth(), columnWidth(0),
viewport()->height() + horizontalHeader()->height());
}
#ifndef MEMORYTABLE_H
#define MEMORYTABLE_H
#include <QObject>
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QTableView>
class MemoryTable : public QTableView {
Q_OBJECT
public:
explicit MemoryTable(QWidget *parent = nullptr);
MemoryTable(QAbstractItemModel * model);
~MemoryTable();
void init(QAbstractItemModel *model);
protected:
void resizeEvent(QResizeEvent *event) override;
QModelIndex moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers) override;
void scrollTo (const QModelIndex & index, ScrollHint hint = EnsureVisible) override;
private:
QTableView *fieldNameTableView;
void init();
void updateRegisterNamesTableGeometry();
private slots:
void updateSectionWidth(int logicalIndex, int oldSize, int newSize);
void updateSectionHeight(int logicalIndex, int oldSize, int newSize);
};
#endif // MEMORYTABLE_H
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>SIMULinho</class>
<widget class="QMainWindow" name="SIMULinho">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1024</width>
<height>640</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>1024</width>
<height>640</height>
</size>
</property>
<property name="windowTitle">
<string>SIMUlinho</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="tabMemoria">
<attribute name="title">
<string>Memória</string>
</attribute>
<widget class="QWidget" name="layoutWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>3</y>
<width>1001</width>
<height>551</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="sizeConstraint">
<enum>QLayout::SetMaximumSize</enum>
</property>
<item>
<widget class="MemoryTable" name="registersTableView"/>
</item>
<item>
<widget class="QTableView" name="tableView_2"/>
</item>
<item>
<widget class="QTableView" name="tableView_3"/>
</item>
</layout>
</widget>
</widget>
<widget class="QWidget" name="tabAlu">
<attribute name="title">
<string>ALUs</string>
</attribute>
</widget>
<widget class="QWidget" name="tabDecodificadorDeInstrucoes">
<attribute name="title">
<string>Decodificador de Instruções</string>
</attribute>
</widget>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1024</width>
<height>25</height>
</rect>
</property>
<widget class="QMenu" name="menuAjuda">
<property name="title">
<string>Ajuda</string>
</property>
<addaction name="actionSobre"/>
</widget>
<widget class="QMenu" name="menuUnidades">
<property name="title">
<string>Módulos (Unidades)</string>
</property>
<addaction name="actionDecodificador_de_Instru_o"/>
<addaction name="separator"/>
<addaction name="actionRegistradores"/>
<addaction name="actionMemoriaDePrograma"/>
<addaction name="actionMemriaDeDados"/>
<addaction name="separator"/>
<addaction name="actionALUInteger"/>
<addaction name="separator"/>
<addaction name="actionDataBaseControlUnit"/>
</widget>
<widget class="QMenu" name="menuSair">
<property name="title">
<string>Controle</string>
</property>
<addaction name="actionSair"/>
</widget>
<addaction name="menuAjuda"/>
<addaction name="menuUnidades"/>
<addaction name="menuSair"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="actionSobre">
<property name="text">
<string>Sobre</string>
</property>
</action>
<action name="actionRegistradores">
<property name="text">
<string>Registradores</string>
</property>
</action>
<action name="actionDecodificador_de_Instru_o">
<property name="text">
<string>Decodificador de Instrução</string>
</property>
</action>
<action name="actionALUInteger">
<property name="text">
<string>ALU Integer</string>
</property>
</action>
<action name="actionDataBaseControlUnit">
<property name="text">
<string>Data Base Control Unit (DBCu)</string>
</property>
</action>
<action name="actionMemoriaDePrograma">
<property name="text">
<string>Memória de Programa</string>
</property>
</action>
<action name="actionMemriaDeDados">
<property name="text">
<string>Memória de Dados</string>
</property>
</action>
<action name="actionSair">
<property name="text">
<string>Sair</string>
</property>
<property name="statusTip">
<string>Saír da Interface e interromper a simulação.</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
<class>MemoryTable</class>
<extends>QTableView</extends>
<header location="global">memorytable.h</header>
<slots>
<signal>signal1()</signal>
<slot>slot1()</slot>
</slots>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment