いわて駐在研究日誌

OpenCAE、電子工作、R/C等、徒然なるままに

PyQtでGUIアプリ(5)

各TabにLayoutを配置するのと、スクロールバーの設定がよくわからない.....。
PyQtは、詳しい日本語サイトが全然ないので結構疲れる。
(あってもせいぜいTutorialをやりましたという程度)

#!/usr/bin/python
# -*- coding: utf-8 -*-

# すべての変数を一つのQWidgetに表示するバージョン

import sys, os, codecs
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class readwriteInc(QMainWindow):

    def __init__(self, parent=None):
        super(readwriteInc, self).__init__(parent)

        self.setWindowTitle("Main Window")
        #self.resize(800, 600)
        #self.setMinimumSize(800, 600)

        # パラメータセクションリスト(Widgetの名前)
        self.sectionList = 
        # パラメータラベルリスト
        self.paramLabelList = 
        # パラメータリスト (変数の値)
        self.parameterList = 
        # コメントリスト(変数のコメント(1行)のリスト)
        self.commentList = 

        # Setup Actions
        ## openFile Action
        openFile = QAction(QIcon('open.png'), 'Open', self)
        openFile.setShortcut('Ctrl+O')
        openFile.setStatusTip('Open File')
        self.connect(openFile, SIGNAL('triggered()'), self.OpenDialog)
        ## saveFile Action
        saveFile = QAction(QIcon('save.png'), 'Save', self)
        saveFile.setShortcut('Ctrl+S')
        saveFile.setStatusTip('Save File')
        self.connect(saveFile, SIGNAL('triggered()'), self.SaveDialog)
        ## Quit Action
        Quit = QAction(QIcon('quit.png'), 'Quit', self)
        Quit.setShortcut('Ctrl+Q')
        Quit.setStatusTip('Quit this program')
        self.connect(Quit, SIGNAL('triggered()'), self.QuitDialog)

        # 1.set Menu Bar
        self.file_menu = self.menuBar().addMenu("&File")
        ## Add  Actions to Menu Bar
        self.file_menu.addAction(openFile)
        self.file_menu.addAction(saveFile)
        self.file_menu.addAction(Quit)

        # 2.set Tool Bar
        self.file_tool = self.addToolBar("ToolBar")
        self.file_tool.addAction(openFile)
        self.file_tool.addAction(saveFile)
        self.file_tool.addAction(Quit)

        # 3.set Status Bar
        self.status_bar = QStatusBar(self)
        self.status_bar.showMessage("Ready", 5000)
        self.setStatusBar(self.status_bar)

        # ScrollAreaを配置
        self.scrollArea = QScrollArea()

        # base Widgetを配置
        self.base = QWidget()
        self.scrollArea.setWidget(self.base)
        self.setCentralWidget(self.base)

    def OpenDialog(self):

        filename = QFileDialog.getOpenFileName(self, 'Open file', '')
        from os.path import isfile
        if isfile(filename):
            fname = codecs.open(filename,'r','utf-8-sig') #UTF-8(BOM対応)のファイルを読み込み
            sectionID = -1
            for line in fname.readlines():

                # ![***] ならばセクション
                if line.strip().startswith("![") and line.strip().endswith("]"):
                    sectionName = line.split("![")[1].split("]")[0].strip()
                    self.sectionList.append(sectionName)
                    sectionID = sectionID + 1

                # :: あるいは = が含まれていればパラメータ指定行
                elif "::" in line or "=" in line:
                    (head, tail1) = line.split('::')
                    (varname, tail2) = tail1.split('=')
                    (var, comment) = tail2.split(' !')

                    self.paramLabelList.append([varname.strip(), sectionID])
                    self.parameterList.append([var.strip(), sectionID])
                    self.commentList.append([comment.strip(), sectionID])

                # それ以外は読みとばす
                else:
                    continue

            fname.close()
            self.setWindowTitle("Main Window: %s" % os.path.abspath(filename))

            # Layoutの配置
            vbox = QVBoxLayout()
            self.base.setLayout(vbox)

            sectionID = 0
            for sectionName in self.sectionList:

               hbox = QHBoxLayout()
               hbox.addWidget(QLabel(""+sectionName+": "))
               vbox.addLayout(hbox)
               # Parameter用Hboxを配置

               paramID = 0
               for line in self.paramLabelList:
                  if line[1] == sectionID:
                      hbox = QHBoxLayout()
                      hbox.addWidget(QLabel(line[0]+" = "))
                      le = QLineEdit()
                      le.setText(self.parameterList[paramID][0])
                      hbox.addWidget(le)
                      hbox.addWidget(QLabel(" : "+self.commentList[paramID][0]))
                      vbox.addLayout(hbox)
                      paramID = paramID + 1

               sectionID = sectionID + 1

    def SaveDialog(self):

        savename = QFileDialog.getSaveFileName(self, 'Save file', '')
        fname = codecs.open(savename,'w','utf-8-sig') #UTF-8のファイルに書き込み

        fname.write("real(8), parameter :: "+self.desc1.text() +" = "+self.parm1.text()+"\n")
        fname.write("real(8), parameter :: "+self.desc2.text() +" = "+self.parm2.text()+"\n")

    def QuitDialog(self):

        reply = QMessageBox.question(self, 'Message',
            "Are you sure to quit?", QMessageBox.Yes |
                  QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            quit()
        else:
            return


def main():

    app = QApplication(sys.argv)
    win = readwriteInc()
    win.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()