Commit 5a90d8c1 authored by Michael Büsch's avatar Michael Büsch

gui: Add abstract config widget

Signed-off-by: Michael Büsch's avatarMichael Buesch <m@bues.ch>
parent 6a0013a7
# -*- coding: utf-8 -*-
#
# AWL simulator - Abstract configuration widget
#
# Copyright 2014 Michael Buesch <m@bues.ch>
#
# 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; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
from __future__ import division, absolute_import, print_function, unicode_literals
from awlsim.common.compat import *
from awlsim.gui.util import *
from awlsim.gui.icons import *
class AbstractConfigDialog(QDialog):
# Signal: Emitted, if any setting changed.
settingsChanged = Signal()
def __init__(self, project, iconName, title,
centralWidget, parent=None):
QDialog.__init__(self, parent)
self.setWindowTitle("Awlsim - " + title)
self.setLayout(QGridLayout(self))
self.project = project
self.centralWidget = centralWidget
hbox = QHBoxLayout()
label = QLabel(self)
label.setPixmap(getIcon(iconName).pixmap(QSize(48, 48)))
hbox.addWidget(label)
label = QLabel(title, self)
font = label.font()
font.setPointSize(max(12, font.pointSize()))
label.setFont(font)
label.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
hbox.addWidget(label)
hbox.addStretch()
self.layout().addLayout(hbox, 0, 0, 1, 4)
self.layout().addWidget(centralWidget, 1, 0, 1, 4)
self.acceptButton = QPushButton("&Accept", self)
self.layout().addWidget(self.acceptButton, 2, 0, 1, 3)
self.cancelButton = QPushButton("&Cancel", self)
self.layout().addWidget(self.cancelButton, 2, 3, 1, 1)
self.loadFromProject()
self.acceptButton.released.connect(self.accept)
self.cancelButton.released.connect(self.reject)
self.accepted.connect(self.storeToProject)
def loadFromProject(self):
raise NotImplementedError
def storeToProject(self):
raise NotImplementedError
......@@ -22,8 +22,8 @@
from __future__ import division, absolute_import, print_function, unicode_literals
from awlsim.common.compat import *
from awlsim.gui.configdialog import *
from awlsim.gui.util import *
from awlsim.gui.icons import *
class ClockMemSpinBox(QSpinBox):
......@@ -45,49 +45,41 @@ class ClockMemSpinBox(QSpinBox):
return -1
return QSpinBox.valueFromText(self, text)
class CpuConfigDialog(QDialog):
def __init__(self, parent):
QDialog.__init__(self, parent)
self.setWindowTitle("CPU configuration")
self.setLayout(QGridLayout(self))
label = QLabel(self)
label.setPixmap(getIcon("prefs").pixmap(QSize(48, 48)))
self.layout().addWidget(label, 0, 0)
class CpuConfigWidget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setLayout(QGridLayout())
self.layout().setContentsMargins(QMargins())
label = QLabel("Number of accumulator registers", self)
self.layout().addWidget(label, 1, 0)
self.layout().addWidget(label, 0, 0)
self.accuCombo = QComboBox(self)
self.accuCombo.addItem("2 accus", 2)
self.accuCombo.addItem("4 accus", 4)
self.layout().addWidget(self.accuCombo, 1, 1)
self.layout().addWidget(self.accuCombo, 0, 1)
label = QLabel("Clock memory byte", self)
self.layout().addWidget(label, 2, 0)
self.layout().addWidget(label, 1, 0)
self.clockMemSpin = ClockMemSpinBox(self)
self.layout().addWidget(self.clockMemSpin, 2, 1)
self.layout().addWidget(self.clockMemSpin, 1, 1)
label = QLabel("Mnemonics", self)
self.layout().addWidget(label, 3, 0)
label = QLabel("Mnemonics language", self)
self.layout().addWidget(label, 2, 0)
self.mnemonicsCombo = QComboBox(self)
self.mnemonicsCombo.addItem("Automatic", S7CPUSpecs.MNEMONICS_AUTO)
self.mnemonicsCombo.addItem("English", S7CPUSpecs.MNEMONICS_EN)
self.mnemonicsCombo.addItem("German", S7CPUSpecs.MNEMONICS_DE)
self.layout().addWidget(self.mnemonicsCombo, 3, 1)
self.layout().addWidget(self.mnemonicsCombo, 2, 1)
self.obTempCheckBox = QCheckBox("Enable writing of OB TEMP "
"entry-variables", self)
self.layout().addWidget(self.obTempCheckBox, 4, 0, 1, 2)
self.layout().addWidget(self.obTempCheckBox, 3, 0, 1, 2)
self.extInsnsCheckBox = QCheckBox("Enable extended "
"non-standard instructions", self)
self.layout().addWidget(self.extInsnsCheckBox, 5, 0, 1, 2)
self.closeButton = QPushButton("Close", self)
self.layout().addWidget(self.closeButton, 6, 1)
self.layout().addWidget(self.extInsnsCheckBox, 4, 0, 1, 2)
self.closeButton.released.connect(self.accept)
self.layout().setRowStretch(5, 1)
def loadFromProject(self, project):
specs = project.getCpuSpecs()
......@@ -112,7 +104,9 @@ class CpuConfigDialog(QDialog):
Qt.Unchecked
)
def saveToProject(self, project):
def storeToProject(self, project):
specs = project.getCpuSpecs()
mnemonics = self.mnemonicsCombo.itemData(self.mnemonicsCombo.currentIndex())
nrAccus = self.accuCombo.itemData(self.accuCombo.currentIndex())
clockMemByte = self.clockMemSpin.value()
......@@ -125,3 +119,21 @@ class CpuConfigDialog(QDialog):
specs.setClockMemByte(clockMemByte)
project.setObTempPresetsEn(obTempEnabled)
project.setExtInsnsEn(extInsnsEnabled)
return True
class CpuConfigDialog(AbstractConfigDialog):
def __init__(self, project, parent=None):
AbstractConfigDialog.__init__(self,
project = project,
iconName = "cpu",
title = "CPU setup",
centralWidget = CpuConfigWidget(),
parent = parent)
def loadFromProject(self):
self.centralWidget.loadFromProject(self.project)
def storeToProject(self):
if self.centralWidget.storeToProject(self.project):
self.settingsChanged.emit()
......@@ -437,9 +437,9 @@ class CpuWidget(QWidget):
def __goOnline(self):
project = self.mainWidget.getProject()
if LinkConfigDialog.askWhenConnecting():
if LinkConfigWidget.askWhenConnecting():
dlg = LinkConfigDialog(project, self)
dlg.contentChanged.connect(self.configChanged)
dlg.settingsChanged.connect(self.configChanged)
if dlg.exec_() != LinkConfigDialog.Accepted:
self.onlineButton.setChecked(False)
return
......
......@@ -22,20 +22,15 @@
from __future__ import division, absolute_import, print_function, unicode_literals
from awlsim.common.compat import *
from awlsim.gui.configdialog import *
from awlsim.gui.util import *
from awlsim.gui.icons import *
class GuiConfigDialog(QDialog):
def __init__(self, parent):
QDialog.__init__(self, parent)
self.setWindowTitle("User interface configuration")
self.setLayout(QGridLayout(self))
label = QLabel(self)
label.setPixmap(getIcon("prefs").pixmap(QSize(48, 48)))
self.layout().addWidget(label, 0, 0)
class GuiConfigWidget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setLayout(QGridLayout())
self.layout().setContentsMargins(QMargins())
self.editGroup = QGroupBox("Source code editor")
self.editGroup.setLayout(QVBoxLayout())
......@@ -61,12 +56,10 @@ class GuiConfigDialog(QDialog):
self.__updateEditFontLabel()
self.editGroup.layout().addLayout(hbox)
self.layout().addWidget(self.editGroup, 1, 0)
self.layout().addWidget(self.editGroup, 0, 0)
self.closeButton = QPushButton("Close", self)
self.layout().addWidget(self.closeButton, 2, 0)
self.layout().setRowStretch(1, 1)
self.closeButton.released.connect(self.accept)
self.editFontButton.released.connect(self.__openEditFontDialog)
def __updateEditFontLabel(self):
......@@ -105,7 +98,7 @@ class GuiConfigDialog(QDialog):
self.__editFont.setStyleHint(QFont.Courier)
self.__updateEditFontLabel()
def saveToProject(self, project):
def storeToProject(self, project):
autoIndentEn = self.editAutoIndent.checkState() == Qt.Checked
pasteIndentEn = self.pasteIndent.checkState() == Qt.Checked
validationEn = self.editValidate.checkState() == Qt.Checked
......@@ -115,3 +108,21 @@ class GuiConfigDialog(QDialog):
guiSettings.setEditorPasteIndentEn(pasteIndentEn)
guiSettings.setEditorValidationEn(validationEn)
guiSettings.setEditorFont(self.__editFont.toString())
return True
class GuiConfigDialog(AbstractConfigDialog):
def __init__(self, project, parent=None):
AbstractConfigDialog.__init__(self,
project = project,
iconName = "prefs",
title = "User interface setup",
centralWidget = GuiConfigWidget(),
parent = parent)
def loadFromProject(self):
self.centralWidget.loadFromProject(self.project)
def storeToProject(self):
if self.centralWidget.storeToProject(self.project):
self.settingsChanged.emit()
......@@ -22,8 +22,8 @@
from __future__ import division, absolute_import, print_function, unicode_literals
from awlsim.common.compat import *
from awlsim.gui.configdialog import *
from awlsim.gui.util import *
from awlsim.gui.icons import *
import sys
......@@ -44,6 +44,9 @@ class _SpawnConfigWidget(QGroupBox):
label.setToolTip(toolTip)
self.layout().addWidget(label, 0, 0)
self.interpreterList = QLineEdit(self)
font = getDefaultFixedFont()
font.setPointSize(self.interpreterList.font().pointSize())
self.interpreterList.setFont(font)
self.interpreterList.setToolTip(toolTip)
self.layout().addWidget(self.interpreterList, 0, 1)
......@@ -113,85 +116,47 @@ class _ConnectConfigWidget(QGroupBox):
self.timeout.setToolTip(toolTip)
self.layout().addWidget(self.timeout, 2, 1)
class LinkConfigDialog(QDialog):
# Signal: Emitted, if any content changed.
contentChanged = Signal()
def __init__(self, project, parent=None):
QDialog.__init__(self, parent)
self.setWindowTitle("Awlsim core connection setup")
self.setLayout(QGridLayout(self))
hbox = QHBoxLayout()
label = QLabel(self)
label.setPixmap(getIcon("network").pixmap(QSize(48, 48)))
hbox.addWidget(label)
label = QLabel("Server connection", self)
font = label.font()
font.setPointSize(max(12, font.pointSize()))
label.setFont(font)
label.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
hbox.addWidget(label)
self.layout().addLayout(hbox, 0, 0)
class LinkConfigWidget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setLayout(QGridLayout())
self.layout().setContentsMargins(QMargins())
group = QGroupBox("Operation mode", self)
group.setLayout(QVBoxLayout())
self.spawnRadio = QRadioButton("Start a simulator core", group)
self.spawnRadio = QRadioButton("Start a &simulator core", group)
self.spawnRadio.setToolTip("This will start a simulator core "
"on-the-fly in the background.\n\n"
"---> If you don't know what to do, select this. <---")
self.spawnRadio.setChecked(True)
group.layout().addWidget(self.spawnRadio)
self.connRadio = QRadioButton("Connect to an external core", group)
self.connRadio = QRadioButton("Connect to an &external core", group)
self.connRadio.setToolTip("Connect to an already running core server.\n"
"This server may be running locally or "
"somewhere else on the (trusted) network.")
group.layout().addWidget(self.connRadio)
self.layout().addWidget(group, 1, 0, 1, 2)
self.layout().addWidget(group, 0, 0, 1, 2)
self.spawnConfig = _SpawnConfigWidget(self)
self.layout().addWidget(self.spawnConfig, 2, 0, 1, 2)
self.layout().addWidget(self.spawnConfig, 1, 0, 1, 2)
self.connConfig = _ConnectConfigWidget(self)
self.connConfig.hide()
self.layout().addWidget(self.connConfig, 3, 0, 1, 2)
self.layout().addWidget(self.connConfig, 2, 0, 1, 2)
self.layout().setRowStretch(3, 1)
self.askCheckBox = QCheckBox("Always ask", self)
self.askCheckBox = QCheckBox("Always as&k", self)
self.askCheckBox.setCheckState(Qt.Checked if
self.askWhenConnecting() else Qt.Unchecked)
self.askCheckBox.setToolTip("Always open this dialog when "
"trying to connect to a CPU.")
self.layout().addWidget(self.askCheckBox, 4, 0, 1, 2)
self.acceptButton = QPushButton("Accept", self)
self.layout().addWidget(self.acceptButton, 5, 0)
self.cancelButton = QPushButton("Cancel", self)
self.layout().addWidget(self.cancelButton, 5, 1)
self.spawnRadio.toggled.connect(self.__spawnToggled)
self.connRadio.toggled.connect(self.__connToggled)
self.__loadFromProject(project)
self.acceptButton.released.connect(self.accept)
self.cancelButton.released.connect(self.reject)
self.accepted.connect(self.__handleAccepted)
self.askCheckBox.stateChanged.connect(self.__askChanged)
def __spawnToggled(self, state):
if state:
self.spawnConfig.show()
else:
self.spawnConfig.hide()
def __connToggled(self, state):
if state:
self.connConfig.show()
else:
self.connConfig.hide()
@classmethod
def askWhenConnecting(cls):
settings = QSettings()
......@@ -205,8 +170,19 @@ class LinkConfigDialog(QDialog):
settings.setValue("connect_ask_details",
1 if newState == Qt.Checked else 0)
def __loadFromProject(self, project):
self.__project = project
def __spawnToggled(self, state):
if state:
self.spawnConfig.show()
else:
self.spawnConfig.hide()
def __connToggled(self, state):
if state:
self.connConfig.show()
else:
self.connConfig.hide()
def loadFromProject(self, project):
linkSettings = project.getCoreLinkSettings()
if linkSettings.getSpawnLocalEn():
......@@ -227,8 +203,8 @@ class LinkConfigDialog(QDialog):
self.connConfig.timeout.setValue(
linkSettings.getConnectTimeoutMs() / 1000.0)
def __handleAccepted(self):
linkSettings = self.__project.getCoreLinkSettings()
def storeToProject(self, project):
linkSettings = project.getCoreLinkSettings()
changed = False
spawnLocalEn = bool(self.spawnRadio.isChecked())
......@@ -258,5 +234,20 @@ class LinkConfigDialog(QDialog):
linkSettings.setConnectTimeoutMs(timeout)
changed = True
if changed:
self.contentChanged.emit()
return changed
class LinkConfigDialog(AbstractConfigDialog):
def __init__(self, project, parent=None):
AbstractConfigDialog.__init__(self,
project = project,
iconName = "network",
title = "Server connection setup",
centralWidget = LinkConfigWidget(),
parent = parent)
def loadFromProject(self):
self.centralWidget.loadFromProject(self.project)
def storeToProject(self):
if self.centralWidget.storeToProject(self.project):
self.settingsChanged.emit()
......@@ -211,27 +211,20 @@ class MainWidget(QWidget):
return self.saveFile(self.filename)
def guiConfig(self):
project = self.getProject()
dlg = GuiConfigDialog(self)
dlg.loadFromProject(project)
dlg = GuiConfigDialog(self.getProject(), self)
dlg.settingsChanged.connect(self.__somethingChanged)
if dlg.exec_() == dlg.Accepted:
dlg.saveToProject(project)
self.projectWidget.setSettings(project.getGuiSettings())
self.__somethingChanged()
self.projectWidget.setSettings(self.getProject().getGuiSettings())
def linkConfig(self):
project = self.getProject()
dlg = LinkConfigDialog(project, self)
dlg.contentChanged.connect(self.__somethingChanged)
dlg = LinkConfigDialog(self.getProject(), self)
dlg.settingsChanged.connect(self.__somethingChanged)
dlg.exec_()
def cpuConfig(self):
project = self.getProject()
dlg = CpuConfigDialog(self)
dlg.loadFromProject(project)
if dlg.exec_() == dlg.Accepted:
dlg.saveToProject(project)
self.__somethingChanged()
dlg = CpuConfigDialog(self.getProject(), self)
dlg.settingsChanged.connect(self.__somethingChanged)
dlg.exec_()
def insertOB(self):
self.projectWidget.insertOB()
......@@ -333,12 +326,12 @@ class MainWindow(QMainWindow):
menu = QMenu("&Settings", self)
menu.addAction(getIcon("network"), "&Server connection...", self.linkConfig)
menu.addAction(getIcon("prefs"), "&CPU config...", self.cpuConfig)
menu.addAction(getIcon("cpu"), "&CPU config...", self.cpuConfig)
menu.addAction(getIcon("prefs"), "&User interface...", self.guiConfig)
self.menuBar().addMenu(menu)
menu = QMenu("&Help", self)
menu.addAction(getIcon("browser"), "Project &homepage...", self.projectHome)
menu.addAction(getIcon("browser"), "Awlsim &homepage...", self.awlsimHomepage)
menu.addSeparator()
menu.addAction(getIcon("cpu"), "&About...", self.about)
self.menuBar().addMenu(menu)
......@@ -501,7 +494,7 @@ class MainWindow(QMainWindow):
ev.accept()
QMainWindow.closeEvent(self, ev)
def projectHome(self):
def awlsimHomepage(self):
QDesktopServices.openUrl(QUrl(AWLSIM_HOME_URL, QUrl.StrictMode))
def about(self):
......
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