Ignore:
Timestamp:
Feb 8, 2021 4:14:27 PM (3 years ago)
Author:
schwenkel
Message:

update palm_gf to python3

File:
1 edited

Legend:

Unmodified
Added
Removed
  • palm/trunk/SCRIPTS/palm_gf_files/palm_gf_tools.py

    r4843 r4869  
    11#!/usr/bin/env python
    2 # -*- coding: utf-8 -*-
    3 #--------------------------------------------------------------------------------#
    4 # This file is part of the PALM model system.
    5 #
    6 # PALM is free software: you can redistribute it and/or modify it under the terms
    7 # of the GNU General Public License as published by the Free Software Foundation,
    8 # either version 3 of the License, or (at your option) any later version.
    9 #
    10 # PALM is distributed in the hope that it will be useful, but WITHOUT ANY
    11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
    12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
    13 #
    14 # You should have received a copy of the GNU General Public License along with
    15 # PALM. If not, see <http://www.gnu.org/licenses/>.
    16 #
    17 # Copyright 1997-2021  Leibniz Universitaet Hannover
    18 #--------------------------------------------------------------------------------#
    19 #
    20 # Current revisions:
    21 # -----------------
    22 #
    23 #
    24 # Former revisions:
    25 # -----------------
    26 # $Id$
    27 # Initial revision
    28 #
    29 #
    30 #
    31 #
    32 #
    33 # Description:
    34 # ------------
    35 #
    36 #
    37 # Instructions:
    38 # -------------
    39 #
    40 #------------------------------------------------------------------------------!
    41 
     2from PyQt5 import QtWidgets, uic
     3from PyQt5.QtWidgets import QFileDialog
    424import sys
     5import sqlite3
    436import os
    44 import sqlite3
    45 from PyQt4 import QtCore, QtGui, uic
    46 import subprocess as sub
    47 import palm_gf_conf as configwr
    48    
     7
    498palm_dir = os.getcwd()
    509palm_bin = palm_dir + '/trunk/SCRIPTS'
    5110
    52 qtCreatorFile = palm_bin + '/palm_gf_files/palm_gf_table.ui'
    53 
    54 Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
    55 
    56 class MyApp3(QtGui.QMainWindow, Ui_MainWindow):
    57 
    58     class MyTableWidgetItem(QtGui.QTableWidgetItem):
     11
     12class MyTable(QtWidgets.QDialog):
     13
     14    class MyTableWidgetItem(QtWidgets.QTableWidgetItem):
    5915        def __init__(self, text, sortKey):
    60             QtGui.QTableWidgetItem.__init__(self, text, QtGui.QTableWidgetItem.UserType)
     16            QtWidgets.QTableWidgetItem.__init__(self, text, QtWidgets.QTableWidgetItem.UserType)
    6117            self.sortKey = sortKey
    6218
     
    6420            return self.sortKey < other.sortKey
    6521
    66     def __init__(self):   #     def __init__(self, parent=None):
    67         QtGui.QMainWindow.__init__(self)
    68         Ui_MainWindow.__init__(self)
    69 
    70         framegm = self.frameGeometry()
    71         screen = QtGui.QApplication.desktop().screenNumber(QtGui.QApplication.desktop().cursor().pos())
    72         centerpoint = QtGui.QApplication.desktop().screenGeometry(screen).center()
    73         framegm.moveCenter(centerpoint)
    74 
    75         #centerpoint = str(centerpoint)
    76         #xcenter = centerpoint.split('(')[1].split(',')[0]
    77         #ycenter = centerpoint.split('(')[1].split(',')[1].split(')')[0]
    78         ##print xcenter, ycenter
    79         #centerpoint = QtCore.QPoint(int(xcenter) + 418, int(ycenter))
    80         #framegm.moveCenter(centerpoint)
    81         self.move(framegm.topLeft())
    82 
    83 
    84 
    85         self.setupUi(self)
    86         self.pushButton_3.clicked.connect(self.load_result)
    87         #self.nx_min.valueChanged.connect(self.check)
    88         self.load_trigger()
    89         self.pushButton.clicked.connect(self.filter_results)
    90         #self.Sortnow_button.clicked.connect(self.sort_order)
    91         self.pushButton_2.clicked.connect(self.load_trigger)
    92         self.tableWidget.horizontalHeader().setClickable(True)
    93 
    94 
     22    def __init__(self):
     23        super(MyTable, self).__init__()
     24        uic.loadUi(palm_bin + '/palm_gf_files/palm_gf_table.ui', self)
     25        self.center_window()
     26        self.load_limits()
     27        self.exec_button.clicked.connect(self.filter_data)
     28        # connects labels of table to sorting subroutine
    9529        self.nx_table.clicked.connect(lambda: self.sort_table(str("nx")))
    9630        self.ny_table.clicked.connect(lambda: self.sort_table(str("ny")))
     
    9832        self.npex_table.clicked.connect(lambda: self.sort_table(str("npex")))
    9933        self.npey_table.clicked.connect(lambda: self.sort_table(str("npey")))
    100         self.npexnpey_table.clicked.connect(lambda: self.sort_table(str("npexnpey")))
     34        self.npexnpey_table.clicked.connect(lambda: self.sort_table(str("pxpy")))
    10135        self.np_table.clicked.connect(lambda: self.sort_table(str("np")))
    10236        self.ngpts_table.clicked.connect(lambda: self.sort_table(str("ngpts")))
    10337        self.nxpex_table.clicked.connect(lambda: self.sort_table(str("nxnpex")))
    10438        self.nypey_table.clicked.connect(lambda: self.sort_table(str("nynpey")))
    105 
    106         self.instant()
    107 
    108         self.save_to_file_button.clicked.connect(self.get_path)
    109 
    110     def instant(self):
    111         checkfile = open(".palm_gf_tmp", "r")
    112         # self.result_label.setText(str(checkfile.readline()))
    113         res_text = str(checkfile.readline())  # XXX
    114         result_nr = res_text.split(' ')[2]
    115         checkfile.close()
    116         if int(result_nr) < 100000:
    117             self.load_result()
    118 
    119 
    120 
    121     def load_trigger(self):
    122         pathx = configwr.read_config()
    123         pathx = pathx[19]
    124 
    125 
    126         dtb = str('.palm_gf_data.db')
    127         #con = sqlite3.connect("/localdata/.palm_gf_data.db")
    128 
    129         pathx = pathx + '/.palm_gf_data.db'
    130         con = sqlite3.connect(pathx)
     39        # connects main buttons to subroutines
     40        self.load_all_button.clicked.connect(self.load_all)
     41        self.reset_button.clicked.connect(self.load_limits)
     42        self.save_button.clicked.connect(self.save)
     43
     44    def center_window(self):
     45        # centers window in the middle of screen
     46        frame = self.frameGeometry()
     47        center = QtWidgets.QDesktopWidget().availableGeometry().center()
     48        frame.moveCenter(center)
     49        self.move(frame.topLeft())
     50
     51    def load_limits(self):
     52        # reads out the .db and sets limits of results as Min/Max
     53        con = sqlite3.connect('palm_gf_data.db')
    13154        c = con.cursor()
    13255        c.execute("SELECT * FROM " + 'grid_limits')
    133         mini = c.fetchone()
    134         max = c.fetchone()
    135         self.nx_min.setValue(mini[0])
    136         self.nx_max.setValue(max[0])
    137 
    138         self.ny_min.setValue(mini[1])
    139         self.ny_max.setValue(max[1])
    140         self.nz_min.setValue(mini[2])
    141         self.nz_max.setValue(max[2])
    142         self.npex_min.setValue(mini[3])
    143         self.npex_max.setValue(max[3])
    144         self.npey_min.setValue(mini[4])
    145         self.npey_max.setValue(max[4])
    146         self.npxnpy_min.setValue(mini[5])
    147         self.npxnpy_max.setValue(max[5])
    148         self.np_min.setValue(mini[6])
    149         self.np_max.setValue(max[6])
    150         self.ngpts_min.setValue(mini[7])
    151         self.ngpts_max.setValue(max[7])
    152         self.nxpex_min.setValue(mini[8])
    153         self.nxpex_max.setValue(max[8])
    154         self.nypey_min.setValue(mini[9])
    155         self.nypey_max.setValue(max[9])
    156 
    157         self.nx_min.setMinimum(mini[0])
    158         self.nx_max.setMaximum(max[0])
    159         self.ny_min.setMinimum(mini[1])
    160         self.ny_max.setMaximum(max[1])
    161         self.nz_min.setMinimum(mini[2])
    162         self.nz_max.setMaximum(max[2])
    163         self.npex_min.setMinimum(mini[3])
    164         self.npex_max.setMaximum(max[3])
    165         self.npey_min.setMinimum(mini[4])
    166         self.npey_max.setMaximum(max[4])
    167         self.npxnpy_min.setMinimum(mini[5])
    168         self.npxnpy_max.setMaximum(max[5])
    169         self.np_min.setMinimum(mini[6])
    170         self.np_max.setMaximum(max[6])
    171 
    172         self.ngpts_min.setMinimum(mini[7])
    173         self.ngpts_max.setMaximum(max[7])
    174         self.ngpts_min.setMaximum(max[7])
    175         self.ngpts_max.setMinimum(mini[7])
    176 
    177         self.nxpex_min.setMinimum(mini[8])
    178         self.nxpex_max.setMaximum(max[8])
    179         self.nxpex_min.setMaximum(max[8])
    180         self.nxpex_max.setMinimum(mini[8])
    181 
    182         self.nypey_min.setMinimum(mini[9])
    183         self.nypey_max.setMaximum(max[9])
    184         self.nypey_min.setMaximum(max[9])
    185         self.nypey_max.setMinimum(mini[9])
    186 
    187 
    188 
    189 
     56        Min = c.fetchone()
     57        Max = c.fetchone()
     58
     59        self.nx_min.setValue(Min[0])
     60        self.nx_min.setMinimum(Min[0])
     61        self.nx_max.setValue(Max[0])
     62        self.nx_max.setMaximum(Max[0])
     63
     64        self.ny_min.setValue(Min[1])
     65        self.ny_min.setMinimum(Min[1])
     66        self.ny_max.setValue(Max[1])
     67        self.ny_max.setMaximum(Max[1])
     68
     69        self.nz_min.setValue(Min[2])
     70        self.nz_min.setMinimum(Min[2])
     71        self.nz_max.setValue(Max[2])
     72        self.nz_max.setMaximum(Max[2])
     73
     74        self.npex_min.setValue(Min[3])
     75        self.npex_min.setMinimum(Min[3])
     76        self.npex_max.setValue(Max[3])
     77        self.npex_max.setMaximum(Max[3])
     78
     79        self.npey_min.setValue(Min[4])
     80        self.npey_min.setMinimum(Min[4])
     81        self.npey_max.setValue(Max[4])
     82        self.npey_max.setMaximum(Max[4])
     83
     84        self.npxnpy_min.setValue(Min[5])
     85        self.npxnpy_min.setMinimum(Min[5])
     86        self.npxnpy_max.setValue(Max[5])
     87        self.npxnpy_max.setMaximum(Max[5])
     88
     89        self.np_min.setValue(Min[6])
     90        self.np_min.setMinimum(Min[6])
     91        self.np_max.setValue(Max[6])
     92        self.np_max.setMaximum(Max[6])
     93        # try statement, as ngpts could be too huge to handle for PyQt5 (>10^8)
     94        try:
     95            self.ngpts_min.setValue(Min[7])
     96            self.ngpts_min.setMinimum(Min[7])
     97            self.ngpts_max.setValue(Max[7])
     98            self.ngpts_max.setMaximum(Max[7])
     99
     100        except OverflowError:
     101            self.ngpts_min.setEnabled(False)
     102            self.ngpts_max.setEnabled(False)
     103            self.ngpts_table.setEnabled(False)
     104            self.ngpts_label.setStyleSheet('color: grey')
     105            self.exec_button.setEnabled(False)
     106            self.status_label.setText('ngpts overflow')
     107
     108        self.nxpex_min.setValue(Min[8])
     109        self.nxpex_min.setMinimum(Min[8])
     110        self.nxpex_max.setValue(Max[8])
     111        self.nxpex_max.setMaximum(Max[8])
     112
     113        self.nypey_min.setValue(Min[9])
     114        self.nypey_min.setMinimum(Min[9])
     115        self.nypey_max.setValue(Max[9])
     116        self.nypey_max.setMaximum(Max[9])
    190117
    191118        con.commit()
     
    193120        con.close()
    194121
    195 
    196 
    197 
    198     def check(self):
    199         pathx = configwr.read_config()
    200         pathx = pathx[19]
    201 
    202         dtb = str('.palm_gf_data.db')
    203         #con = sqlite3.connect("/localdata/.palm_gf_data.db")
    204         con = sqlite3.connect(pathx + '/.palm_gf_data.db')
     122    def load_all(self):
     123        # shows all results found in .db file
     124        con = sqlite3.connect('palm_gf_data.db')
    205125        c = con.cursor()
    206         c.execute("SELECT * FROM " + 'grid_limits')
    207         mini = c.fetchone()
    208         max = c.fetchone()
    209         if self.nx_min.value() < mini[0]:
    210 
    211             self.nx_min.setValue(mini[0])
    212 
    213 
    214     def process1(self):
    215         self.calc_label.setText('loading...')
    216         QtGui.QApplication.processEvents()
    217 
    218 
    219 
    220     def load_result(self):
    221         #print("LOADED!!!")
    222         import decimal
    223 
    224         pathx = configwr.read_config()
    225         pathx = pathx[19]
    226 
    227         self.setEnabled(False)
    228         QtGui.QApplication.processEvents()
    229         self.load_trigger()
    230         self.process1()
    231         database = str('.palm_gf_data.db')
    232         conn = sqlite3.connect(pathx + '/.palm_gf_data.db')
    233         c = conn.cursor()
    234126        c.execute("SELECT * FROM " + 'grid_current')
    235         results = c.fetchall()
    236 
    237         self.tableWidget.setRowCount(len(results))
    238 
    239         i = 0
    240         j = 0
    241         k = 0
    242 
    243         while i < len(results):
    244             line = results[i]
    245             while j < 10:
    246                 var = line[j]
     127        data = c.fetchall()
     128
     129        self.tableWidget.setRowCount(len(data))
     130        row_cnt = -1
     131        for i in range(0, len(data)):
     132            line = data[i]
     133            row_cnt += 1
     134            for j in range(0, 10):
     135                value = line[j]
    247136
    248137                if j == 7:
    249                     self.tableWidget.setItem(i, j, self.MyTableWidgetItem(str("%.1e" % var), j + i))
    250                     #print("%.2e" % int(var), "%.4e" % int(1782))
     138                    self.tableWidget.setItem(row_cnt, j, self.MyTableWidgetItem(
     139                        str("%.1e" % value), i))
    251140
    252141                else:
    253                     self.tableWidget.setItem(i, j, self.MyTableWidgetItem(str(var), j+i))
    254                 #item = self.MyTableWidgetItem(str(var), k)
    255                 #self.tableWidget.setItem(i, j, item)
    256 
    257                 j += 1
    258                 #if j == 3:
    259                     #print(k)
    260                 #k += 1
    261             #k -= 7
    262             k += 1
    263             j = 0
    264             i += 1
     142                    self.tableWidget.setItem(row_cnt, j, self.MyTableWidgetItem(
     143                        str(value), i))
    265144
    266145        c.close()
    267         conn.close()
    268         self.calc_label.setText('loading completed')
    269         self.label_11.setText(str(len(results)) + ' results ')
    270         self.setEnabled(True)
    271         QtGui.QApplication.processEvents()
    272 
    273     def filter_results(self):
    274 
    275         pathx = configwr.read_config()
    276         pathx = pathx[19]
    277 
    278         self.setEnabled(False)
    279         self.calc_label.setText('calculating...')
    280         QtGui.QApplication.processEvents()
    281         database = str('.palm_gf_data.db')
    282         conn = sqlite3.connect(pathx + '/.palm_gf_data.db')
    283         c = conn.cursor()
    284         c.execute("SELECT * FROM " + "grid_current")
    285         results = c.fetchall()
    286         #print(results)
    287 
    288         self.tableWidget.setRowCount(len(results))
    289 
    290         i = 0
    291         j = 0
     146        con.close()
     147        self.tableWidget.setRowCount(row_cnt + 1)
     148        self.status_label.setText(str(row_cnt + 1) + ' results loaded')
     149
     150    def filter_data(self):
     151        # filters data according to set Min/Max. Reads out .db to find all fitting data
     152        con = sqlite3.connect('palm_gf_data.db')
     153        c = con.cursor()
     154        c.execute("SELECT * FROM " + 'grid_current')
     155        data = c.fetchall()
     156
     157        self.tableWidget.setRowCount(len(data))
     158
    292159        row_cnt = -1
    293         while i < len(results):
    294             line = results[i]
    295 
    296             if line[0] <= self.nx_max.value():
    297 
    298                 if line[0] >= self.nx_min.value():
    299 
    300                     if line[1] <= self.ny_max.value():
    301 
    302                         if line[1] >= self.ny_min.value():
    303 
    304                             if line[2] <= self.nz_max.value():
    305 
    306                                 if line[2] >= self.nz_min.value():
    307 
    308                                     if line[3] <= self.npex_max.value():
    309 
    310                                         if line[3] >= self.npex_min.value():
    311 
    312                                             if line[4] <= self.npey_max.value():
    313 
    314                                                 if line[4] >= self.npey_min.value():
    315 
    316                                                     if line[5] <= self.npxnpy_max.value():
    317 
    318                                                         if line[5] >= self.npxnpy_min.value():
    319 
    320                                                             if line[6] <= self.np_max.value():
    321 
    322                                                                 if line[6] >= self.np_min.value():
    323 
    324                                                                     if line[7] <= self.ngpts_max.value():
    325 
    326                                                                         if line[7] >= self.ngpts_min.value():
    327 
    328                                                                             if line[8] <= self.nxpex_max.value():
    329 
    330                                                                                 if line[8] >= self.nxpex_min.value():
    331 
    332                                                                                     if line[9] <= self.nypey_max.value():
    333 
    334                                                                                         if line[9] >= self.nypey_min.value():
    335 
    336                                                                                             row_cnt += 1
    337                                                                                             while j < 10:
    338                                                                                                 var = line[j]
    339 
    340                                                                                                 if j == 7:
    341                                                                                                     self.tableWidget.setItem(row_cnt, j, self.MyTableWidgetItem(str("%.1e" % var), i))
    342 
    343                                                                                                 else:
    344                                                                                                     self.tableWidget.setItem(row_cnt, j, self.MyTableWidgetItem(str(var), i))
    345 
    346                                                                                                 j += 1
    347 
    348             j = 0
    349             i += 1
     160        for i in range(0, len(data)):
     161            line = data[i]
     162            if self.nx_min.value() <= line[0] <= self.nx_max.value():
     163                if self.ny_min.value() <= line[1] <= self.ny_max.value():
     164                    if self.nz_min.value() <= line[2] <= self.nz_max.value():
     165                        if self.npex_min.value() <= line[3] <= self.npex_max.value():
     166                            if self.npey_min.value() <= line[4] <= self.npey_max.value():
     167                                if self.npxnpy_min.value() <= line[5] <= self.npxnpy_max.value():
     168                                    if self.np_min.value() <= line[6] <= self.np_max.value():
     169                                        if self.ngpts_min.value() <= line[7] <= self.ngpts_max.value():
     170                                            if self.nxpex_min.value() <= line[8] <= self.nxpex_max.value():
     171                                                if self.nypey_min.value() <= line[9] <= self.nypey_max.value():
     172                                                    row_cnt += 1
     173                                                    for j in range(0, 10):
     174                                                        value = line[j]
     175
     176                                                        if j == 7:
     177                                                            self.tableWidget.setItem(row_cnt, j, self.MyTableWidgetItem(
     178                                                                str("%.1e" % value), i))
     179                                                        else:
     180                                                            self.tableWidget.setItem(row_cnt, j, self.MyTableWidgetItem(
     181                                                                str(value), i))
    350182
    351183        c.close()
    352         conn.close()
     184        con.close()
    353185        self.tableWidget.setRowCount(row_cnt + 1)
    354         self.setEnabled(True)
    355         self.calc_label.setText('calculation completed')
    356         self.label_11.setText(str(row_cnt + 1) + ' results')
    357         QtGui.QApplication.processEvents()
    358 
    359 
     186        self.status_label.setText(str(row_cnt + 1) + ' results found')
    360187
    361188    def sort_table(self, column):
    362 
     189        # with argument as chosen column, read not from table but from .db and reloads sorted data into table
    363190        fnx_mn = self.nx_min.value()
    364191        fnx_mx = self.nx_max.value()
     
    382209        nypey_mx = self.nypey_max.value()
    383210
    384 
    385         if column == str("nx"):
    386             sorted_col = "nx"
    387 
     211        if column == 'nx':
     212            sorted_col = 'nx'
    388213            if self.nx_table.isChecked() is True:
    389214                order = " DESC"
    390 
    391             else:
    392 
    393                 order = " ASC"
    394 
    395             self.ny_table.setChecked(False)
    396             self.nz_table.setChecked(False)
    397             self.npex_table.setChecked(False)
    398             self.npey_table.setChecked(False)
    399             self.npexnpey_table.setChecked(False)
     215            else:
     216                order = " ASC"
     217            self.ny_table.setChecked(False)
     218            self.nz_table.setChecked(False)
     219            self.npex_table.setChecked(False)
     220            self.npey_table.setChecked(False)
    400221            self.np_table.setChecked(False)
    401222            self.ngpts_table.setChecked(False)
     
    405226        if column == str("ny"):
    406227            sorted_col = "ny"
    407 
    408228            if self.ny_table.isChecked() is True:
    409229                order = " DESC"
    410 
    411             else:
    412 
     230            else:
    413231                order = " ASC"
    414232
     
    425243        if column == str("nz"):
    426244            sorted_col = "nz"
    427 
    428245            if self.nz_table.isChecked() is True:
    429246                order = " DESC"
    430 
    431             else:
    432 
     247            else:
    433248                order = " ASC"
    434249
     
    445260        if column == str("npex"):
    446261            sorted_col = "npex"
    447 
    448262            if self.npex_table.isChecked() is True:
    449263                order = " DESC"
    450 
    451             else:
    452 
     264            else:
    453265                order = " ASC"
    454266
     
    465277        if column == str("npey"):
    466278            sorted_col = "npey"
    467 
    468279            if self.npey_table.isChecked() is True:
    469280                order = " DESC"
    470 
    471             else:
    472 
    473                 order = " ASC"
    474 
    475 
    476             self.ny_table.setChecked(False)
    477             self.nz_table.setChecked(False)
    478             self.npex_table.setChecked(False)
    479             self.nx_table.setChecked(False)
    480             self.npexnpey_table.setChecked(False)
    481             self.np_table.setChecked(False)
    482             self.ngpts_table.setChecked(False)
    483             self.nxpex_table.setChecked(False)
    484             self.nypey_table.setChecked(False)
    485 
    486         if column == str("npexnpey"):
    487             sorted_col = "npxnpy"
    488 
     281            else:
     282                order = " ASC"
     283
     284            self.ny_table.setChecked(False)
     285            self.nz_table.setChecked(False)
     286            self.npex_table.setChecked(False)
     287            self.nx_table.setChecked(False)
     288            self.npexnpey_table.setChecked(False)
     289            self.np_table.setChecked(False)
     290            self.ngpts_table.setChecked(False)
     291            self.nxpex_table.setChecked(False)
     292            self.nypey_table.setChecked(False)
     293
     294        if column == str("pxpy"):
     295            sorted_col = "pxpy"
    489296            if self.npexnpey_table.isChecked() is True:
    490297                order = " DESC"
    491 
    492             else:
    493 
     298            else:
    494299                order = " ASC"
    495300
     
    506311        if column == str("np"):
    507312            sorted_col = "np"
    508 
    509313            if self.np_table.isChecked() is True:
    510314                order = " DESC"
    511 
    512             else:
    513 
     315            else:
    514316                order = " ASC"
    515317
     
    526328        if column == str("ngpts"):
    527329            sorted_col = "ngpts"
    528 
    529330            if self.ngpts_table.isChecked() is True:
    530331                order = " DESC"
    531 
    532             else:
    533 
     332            else:
    534333                order = " ASC"
    535334
     
    546345        if column == str("nxnpex"):
    547346            sorted_col = "nxnpex"
    548 
    549347            if self.nxpex_table.isChecked() is True:
    550348                order = " DESC"
    551 
    552             else:
    553 
     349            else:
    554350                order = " ASC"
    555351
     
    566362        if column == str("nynpey"):
    567363            sorted_col = "nynpey"
    568 
    569364            if self.nypey_table.isChecked() is True:
    570365                order = " DESC"
    571 
    572             else:
    573 
    574                 order = " ASC"
    575 
    576             self.ny_table.setChecked(False)
    577             self.nz_table.setChecked(False)
    578             self.npex_table.setChecked(False)
    579             self.npey_table.setChecked(False)
    580             self.npexnpey_table.setChecked(False)
    581             self.np_table.setChecked(False)
    582             self.nx_table.setChecked(False)
    583             self.ngpts_table.setChecked(False)
    584             self.nxpex_table.setChecked(False)
    585 
    586         else:
    587             pass
    588 
    589         pathx = configwr.read_config()
    590         pathx = pathx[19]
    591 
    592 
    593         conn = sqlite3.connect(pathx + "/.palm_gf_data.db")
     366            else:
     367                order = " ASC"
     368
     369            self.ny_table.setChecked(False)
     370            self.nz_table.setChecked(False)
     371            self.npex_table.setChecked(False)
     372            self.npey_table.setChecked(False)
     373            self.npexnpey_table.setChecked(False)
     374            self.np_table.setChecked(False)
     375            self.nx_table.setChecked(False)
     376            self.ngpts_table.setChecked(False)
     377            self.nxpex_table.setChecked(False)
     378
     379        conn = sqlite3.connect('palm_gf_data.db')
    594380        c = conn.cursor()
    595         c.execute("SELECT * FROM grid_current  WHERE nx <= " + str(fnx_mx) + " AND nx >= "  + str(fnx_mn) + " AND ny <= " + str(fny_mx) + " AND ny >= " + str(fny_mn) + " AND nz <= " + str(fnz_mx) +
     381        c.execute("SELECT * FROM grid_current  WHERE nx <= " + str(fnx_mx) + " AND nx >= " + str(
     382            fnx_mn) + " AND ny <= " + str(fny_mx) + " AND ny >= " + str(fny_mn) + " AND nz <= " + str(fnz_mx) +
    596383                  " AND nz >= " + str(fnz_mn) + " AND npex <= " + str(fnpex_mx) + " AND npex >= " +
    597         str(fnpex_mn) + " AND npey <= " + str(fnpey_mx) + " AND npey >= " + str(fnpey_mn) + " AND "
    598                   "npxnpy <= " + str(fnpxnpy_mx) + " AND npxnpy >= " + str(fnpxnpy_mn) + " AND np <= " + str(fnp_mx) + " AND np >= " + str(fnp_mn) + " AND ngpts <= " + str(fngpts_mx) + " AND ngpts >= " + str(fngpts_mn) +
    599         " AND nxnpex <= " + str(nxpex_mx) + " AND nxnpex >= " + str(nxpex_mn) + " AND nynpey <= " + str(nypey_mx) + " AND nynpey >= " + str(nypey_mn) +
    600         " ORDER BY " + str(sorted_col) + str(order))
    601 
    602 
     384                  str(fnpex_mn) + " AND npey <= " + str(fnpey_mx) + " AND npey >= " + str(fnpey_mn) + " AND "
     385                                                                                                      "pxpy <= " + str(
     386            fnpxnpy_mx) + " AND pxpy >= " + str(fnpxnpy_mn) + " AND np <= " + str(fnp_mx) + " AND np >= " + str(
     387            fnp_mn) + " AND ngpts <= " + str(fngpts_mx) + " AND ngpts >= " + str(fngpts_mn) +
     388                  " AND nxnpex <= " + str(nxpex_mx) + " AND nxnpex >= " + str(nxpex_mn) + " AND nynpey <= " + str(
     389            nypey_mx) + " AND nynpey >= " + str(nypey_mn) +
     390                  " ORDER BY " + str(sorted_col) + str(order))
    603391
    604392        sorted = c.fetchall()
    605 
    606 
    607393        c.close()
    608394        conn.close()
    609395        self.tableWidget.setRowCount(len(sorted))
    610 
    611         for row_indx in range(0,len(sorted)):
    612 
    613             for col_indx in range(0,10):
     396        for row_indx in range(0, len(sorted)):
     397            for col_indx in range(0, 10):
    614398                row = sorted[row_indx]
    615399                value = row[col_indx]
    616400                if col_indx == 7:
    617                     self.tableWidget.setItem(row_indx, col_indx, QtGui.QTableWidgetItem(str("%.1e" % value)))
     401                    self.tableWidget.setItem(row_indx, col_indx, self.MyTableWidgetItem(str("%.1e" % value), col_indx))
    618402                else:
    619                     self.tableWidget.setItem(row_indx, col_indx, QtGui.QTableWidgetItem(str(value)))
    620 
    621 
    622 
    623     def sort_order(self):
    624 
    625         sorted = 0
    626         fnx_mn = self.nx_min.value()
    627         fnx_mx = self.nx_max.value()
    628         fny_mn = self.ny_min.value()
    629         fny_mx = self.ny_max.value()
    630         fnz_mn = self.nz_min.value()
    631         fnz_mx = self.nz_max.value()
    632         fnpex_mn = self.npex_min.value()
    633         fnpex_mx = self.npex_max.value()
    634         fnpey_mn = self.npex_min.value()
    635         fnpey_mx = self.npey_max.value()
    636         fnpxnpy_mn = self.npxnpy_min.value()
    637         fnpxnpy_mx = self.npxnpy_max.value()
    638         fnp_mn = self.np_min.value()
    639         fnp_mx = self.np_max.value()
    640         fngpts_mn = self.ngpts_min.value()
    641         fngpts_mx = self.ngpts_max.value()
    642 
    643         if str(self.Sortvariable_box.currentIndex()) == str(1):
    644 
    645 
    646             sorted_col = "nx"
    647         if str(self.Sortvariable_box.currentIndex()) == str(2):
    648 
    649             sorted_col = "ny"
    650         if str(self.Sortvariable_box.currentIndex()) == str(3):
    651 
    652             sorted_col = "nz"
    653         if str(self.Sortvariable_box.currentIndex()) == str(4):
    654 
    655             sorted_col = "npex"
    656         if str(self.Sortvariable_box.currentIndex()) == str(5):
    657 
    658             sorted_col = "npey"
    659         if str(self.Sortvariable_box.currentIndex()) == str(6):
    660 
    661             sorted_col = "npxnpy"
    662         if str(self.Sortvariable_box.currentIndex()) == str(7):
    663 
    664             sorted_col = "np"
    665         if str(self.Sortvariable_box.currentIndex()) == str(8):
    666 
    667             sorted_col = "ngpts"
    668 
    669         #print(self.Sortvariable_box.currentIndex())
    670 
    671         if str(self.Sortorder_box.currentIndex()) == str(1):
    672 
    673             order = " ASC"
    674 
    675         if str(self.Sortorder_box.currentIndex()) == str(2):
    676 
    677             order = " DESC"
    678 
    679 
    680 
    681         conn = sqlite3.connect("/localdata/.palm_gf_data.db")
    682         c = conn.cursor()
    683         c.execute("SELECT * FROM grid_current  WHERE nx <= " + str(fnx_mx) + " AND nx >= "  + str(fnx_mn) + " AND ny <= " + str(fny_mx) + " AND ny >= " + str(fny_mn) + " AND nz <= " + str(fnz_mx) +
    684                   " AND nz >= " + str(fnz_mn) + " AND npex <= " + str(fnpex_mx) + " AND npex >= " +
    685         str(fnpex_mn) + " AND npey <= " + str(fnpey_mx) + " AND npey >= " + str(fnpey_mn) + " AND "
    686                   "npxnpy <= " + str(fnpxnpy_mx) + " AND npxnpy >= " + str(fnpxnpy_mn) + " AND np <= " + str(fnp_mx) + " AND np >= " + str(fnp_mn) + " AND ngpts <= " + str(fngpts_mx) + " AND ngpts >= " + str(fngpts_mn) +
    687         " ORDER BY " + str(sorted_col) + str(order))
    688 
    689 
    690 
    691         sorted = c.fetchall()
    692 
    693         c.close()
    694         conn.close()
    695         self.tableWidget.setRowCount(len(sorted))
    696 
    697         for row_indx in range(0,len(sorted)):
    698 
    699             for col_indx in range(0,8):
    700                 row = sorted[row_indx]
    701                 value = row[col_indx]
    702                 self.tableWidget.setItem(row_indx, col_indx, QtGui.QTableWidgetItem(str(var)))
    703 
    704 
    705 
    706 
    707 
    708 
    709         #print(len(sorted))
    710         #print(sorted)
    711 
    712     def get_path(wildcard):
    713         import wx
    714         app = wx.App(None)
    715         style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST
    716         dialog = wx.FileDialog(None, 'Open', wildcard='*.db')
    717         if dialog.ShowModal() == wx.ID_OK:
    718             path = dialog.GetPath()
    719 
    720             print(path, file)
    721         else:
    722             path = None
    723         dialog.Destroy()
    724         return
    725 
    726 
    727 
    728 
    729 
    730 
    731 
    732 
    733 
     403                    self.tableWidget.setItem(row_indx, col_indx, self.MyTableWidgetItem(str(value), col_indx))
     404
     405        self.status_label.setText(str(len(sorted)) + ' results found')
     406
     407    def save_dialog(self):
     408        # calling native PyQt5 Dialog to get desired save path
     409        options = QFileDialog.Options()
     410        options |= QFileDialog.DontUseNativeDialog
     411        fileName, _ = QFileDialog.getSaveFileName(self, "Save .txt File", ".txt",
     412                                                  "All Files (*.txt);;Text Files (*.txt)", options=options)
     413        if fileName:
     414            return fileName
     415
     416    def save(self):
     417        # saves the file
     418        # try because QFileDialog could be closed without choosing path
     419        try:
     420            if len(self.tableWidget.selectedItems()) != 0:
     421                location = self.save_dialog()
     422                data2safe = self.tableWidget.selectedItems()
     423                if location is not None:
     424                    with open(location, 'w') as file:
     425                        file.write('nx  ' + 'ny  ' + 'nz  ' + 'npex  ' + 'npey' + "\n")
     426                        for row in range(0, int(len(data2safe)), 10):
     427                            file.write(str(data2safe[0+row].text()) + '  ' + str(data2safe[1+row].text()) + '  ' +
     428                                       str(data2safe[2+row].text()) + '  ' + str(data2safe[3+row].text()) + '  ' +
     429                                       str(data2safe[4+row].text()) + "\n")
     430                    file.close()
     431            else:
     432                self.status_label.setText('Select to save')
     433        except AttributeError:
     434            pass
    734435
    735436
    736437if __name__ == "__main__":
    737     app = QtGui.QApplication(sys.argv)
    738     window = MyApp3()
    739     window.setWindowTitle('Gridfilter')
     438    app = QtWidgets.QApplication(sys.argv)
     439    window = MyTable()
     440    window.setWindowTitle('palm_gf Table')
    740441    window.show()
    741442    sys.exit(app.exec_())
    742 
Note: See TracChangeset for help on using the changeset viewer.