apol: implement default query tab

Closes #121
This commit is contained in:
Chris PeBenito 2016-04-14 10:03:34 -04:00
parent d8256cee4a
commit d09bfdd09b
4 changed files with 869 additions and 1 deletions

643
data/defaultquery.ui Normal file
View File

@ -0,0 +1,643 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>DefaultQueryTab_ui</class>
<widget class="QScrollArea" name="DefaultQueryTab_ui">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>774</width>
<height>846</height>
</rect>
</property>
<property name="sizeAdjustPolicy">
<enum>QAbstractScrollArea::AdjustToContents</enum>
</property>
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="contents">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>772</width>
<height>844</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<layout class="QGridLayout" name="gridLayout_9">
<property name="leftMargin">
<number>6</number>
</property>
<property name="topMargin">
<number>6</number>
</property>
<property name="rightMargin">
<number>6</number>
</property>
<property name="bottomMargin">
<number>6</number>
</property>
<property name="spacing">
<number>3</number>
</property>
<item row="0" column="3">
<widget class="QCheckBox" name="criteria_expander">
<property name="toolTip">
<string>Show or hide the search criteria (no settings are lost)</string>
</property>
<property name="text">
<string>Criteria</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>20</height>
</size>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Default Query</string>
</property>
</widget>
</item>
<item row="0" column="1">
<spacer name="horizontalSpacer_8">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="5" column="0" colspan="5">
<widget class="QTextEdit" name="notes">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>1</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>80</height>
</size>
</property>
<property name="toolTip">
<string>Optionally enter notes here about the query.</string>
</property>
<property name="placeholderText">
<string>Enter notes here.</string>
</property>
</widget>
</item>
<item row="0" column="4">
<widget class="QCheckBox" name="notes_expander">
<property name="toolTip">
<string>Show or hide the notes field (no data is lost)</string>
</property>
<property name="text">
<string>Notes</string>
</property>
</widget>
</item>
<item row="4" column="0" colspan="5">
<widget class="QTabWidget" name="results_frame">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>2</verstretch>
</sizepolicy>
</property>
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="results_framePage1">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<attribute name="title">
<string>Results</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_3">
<property name="leftMargin">
<number>6</number>
</property>
<property name="topMargin">
<number>6</number>
</property>
<property name="rightMargin">
<number>6</number>
</property>
<property name="bottomMargin">
<number>6</number>
</property>
<item>
<widget class="QTableView" name="table_results">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="sizeAdjustPolicy">
<enum>QAbstractScrollArea::AdjustIgnored</enum>
</property>
<property name="alternatingRowColors">
<bool>true</bool>
</property>
<property name="sortingEnabled">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="results_framePage2">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<attribute name="title">
<string>Raw Results</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="leftMargin">
<number>6</number>
</property>
<property name="topMargin">
<number>6</number>
</property>
<property name="rightMargin">
<number>6</number>
</property>
<property name="bottomMargin">
<number>6</number>
</property>
<item>
<widget class="QPlainTextEdit" name="raw_results">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="font">
<font>
<family>Monospace</family>
</font>
</property>
<property name="documentTitle">
<string/>
</property>
<property name="lineWrapMode">
<enum>QPlainTextEdit::NoWrap</enum>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
<item row="0" column="2">
<widget class="QLabel" name="label_2">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Show:</string>
</property>
</widget>
</item>
<item row="1" column="0" colspan="5">
<widget class="QFrame" name="criteria_frame">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>1</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>16777215</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QGroupBox" name="ruletype_criteria">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>150</height>
</size>
</property>
<property name="title">
<string>Rule Type</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<property name="leftMargin">
<number>6</number>
</property>
<property name="topMargin">
<number>6</number>
</property>
<property name="rightMargin">
<number>6</number>
</property>
<property name="bottomMargin">
<number>6</number>
</property>
<property name="spacing">
<number>3</number>
</property>
<item row="0" column="3">
<widget class="QPushButton" name="clear_ruletypes">
<property name="text">
<string>Clear</string>
</property>
<property name="checkable">
<bool>false</bool>
</property>
</widget>
</item>
<item row="0" column="4">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="3">
<widget class="QPushButton" name="all_ruletypes">
<property name="text">
<string>Select All</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QCheckBox" name="default_user">
<property name="enabled">
<bool>true</bool>
</property>
<property name="text">
<string>default_user</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="default_role">
<property name="text">
<string>default_role</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QCheckBox" name="default_type">
<property name="text">
<string>default_type</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QCheckBox" name="default_range">
<property name="text">
<string>default_range</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="2" column="0" colspan="2">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Apply</set>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QGroupBox" name="class_criteria">
<property name="title">
<string>Object Class</string>
</property>
<layout class="QGridLayout" name="gridLayout_7">
<property name="leftMargin">
<number>6</number>
</property>
<property name="topMargin">
<number>6</number>
</property>
<property name="rightMargin">
<number>6</number>
</property>
<property name="bottomMargin">
<number>6</number>
</property>
<property name="spacing">
<number>3</number>
</property>
<item row="0" column="2">
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="1">
<widget class="QPushButton" name="clear_class">
<property name="text">
<string>Clear</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QPushButton" name="invert_class">
<property name="text">
<string>Invert</string>
</property>
</widget>
</item>
<item row="2" column="1">
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="0" rowspan="3">
<widget class="QListView" name="tclass">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>250</width>
<height>16777215</height>
</size>
</property>
<property name="toolTip">
<string>Match the object class of the rule.</string>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::ExtendedSelection</enum>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="1" column="0">
<widget class="QGroupBox" name="default_criteria">
<property name="maximumSize">
<size>
<width>16777215</width>
<height>100</height>
</size>
</property>
<property name="title">
<string>Default</string>
</property>
<layout class="QGridLayout" name="gridLayout_5">
<property name="leftMargin">
<number>6</number>
</property>
<property name="topMargin">
<number>6</number>
</property>
<property name="rightMargin">
<number>6</number>
</property>
<property name="bottomMargin">
<number>6</number>
</property>
<property name="spacing">
<number>3</number>
</property>
<item row="0" column="1">
<widget class="QComboBox" name="default_range_2">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<item>
<property name="text">
<string/>
</property>
</item>
<item>
<property name="text">
<string>low</string>
</property>
</item>
<item>
<property name="text">
<string>high</string>
</property>
</item>
<item>
<property name="text">
<string>low_high</string>
</property>
</item>
</widget>
</item>
<item row="0" column="0">
<widget class="QComboBox" name="default_2">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<item>
<property name="text">
<string/>
</property>
</item>
<item>
<property name="text">
<string>source</string>
</property>
</item>
<item>
<property name="text">
<string>target</string>
</property>
</item>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</widget>
<tabstops>
<tabstop>criteria_expander</tabstop>
<tabstop>notes_expander</tabstop>
<tabstop>default_user</tabstop>
<tabstop>default_type</tabstop>
<tabstop>default_role</tabstop>
<tabstop>default_range</tabstop>
<tabstop>clear_ruletypes</tabstop>
<tabstop>all_ruletypes</tabstop>
<tabstop>tclass</tabstop>
<tabstop>clear_class</tabstop>
<tabstop>invert_class</tabstop>
<tabstop>default_2</tabstop>
<tabstop>default_range_2</tabstop>
<tabstop>results_frame</tabstop>
<tabstop>table_results</tabstop>
<tabstop>raw_results</tabstop>
<tabstop>notes</tabstop>
</tabstops>
<resources/>
<connections>
<connection>
<sender>criteria_expander</sender>
<signal>toggled(bool)</signal>
<receiver>criteria_frame</receiver>
<slot>setVisible(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>629</x>
<y>16</y>
</hint>
<hint type="destinationlabel">
<x>379</x>
<y>239</y>
</hint>
</hints>
</connection>
<connection>
<sender>notes_expander</sender>
<signal>toggled(bool)</signal>
<receiver>notes</receiver>
<slot>setVisible(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>735</x>
<y>16</y>
</hint>
<hint type="destinationlabel">
<x>386</x>
<y>776</y>
</hint>
</hints>
</connection>
<connection>
<sender>clear_class</sender>
<signal>clicked()</signal>
<receiver>tclass</receiver>
<slot>clearSelection()</slot>
<hints>
<hint type="sourcelabel">
<x>318</x>
<y>164</y>
</hint>
<hint type="destinationlabel">
<x>150</x>
<y>188</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@ -28,6 +28,7 @@ from .boundsquery import BoundsQueryTab
from .categoryquery import CategoryQueryTab
from .commonquery import CommonQueryTab
from .constraintquery import ConstraintQueryTab
from .defaultquery import DefaultQueryTab
from .dta import DomainTransitionAnalysisTab
from .fsusequery import FSUseQueryTab
from .genfsconquery import GenfsconQueryTab
@ -87,7 +88,8 @@ class ChooseAnalysis(SEToolsWidget, QDialog):
"Nodecon Statements": NodeconQueryTab,
"Portcon Statements": PortconQueryTab}
general_choices = {"Summary": SummaryTab}
other_choices = {"Bounds": BoundsQueryTab}
other_choices = {"Bounds": BoundsQueryTab,
"Defaults": DefaultQueryTab}
analysis_choices = {"Components": components_map,
"Rules": rule_map,
"Analyses": analysis_map,

View File

@ -0,0 +1,171 @@
# Copyright 2016, Tresys Technology, LLC
#
# This file is part of SETools.
#
# SETools is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 2.1 of
# the License, or (at your option) any later version.
#
# SETools 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with SETools. If not, see
# <http://www.gnu.org/licenses/>.
#
import logging
from PyQt5.QtCore import Qt, QSortFilterProxyModel, QStringListModel, QThread
from PyQt5.QtGui import QPalette, QTextCursor
from PyQt5.QtWidgets import QCompleter, QHeaderView, QMessageBox, QProgressDialog, QScrollArea
from setools import DefaultQuery
from ..logtosignal import LogHandlerToSignal
from ..models import SEToolsListModel, invert_list_selection
from ..defaultmodel import DefaultTableModel
from ..widget import SEToolsWidget
from .queryupdater import QueryResultsUpdater
class DefaultQueryTab(SEToolsWidget, QScrollArea):
"""Default browser and query tab."""
def __init__(self, parent, policy, perm_map):
super(DefaultQueryTab, self).__init__(parent)
self.log = logging.getLogger(__name__)
self.policy = policy
self.query = DefaultQuery(policy)
self.setupUi()
def __del__(self):
self.thread.quit()
self.thread.wait(5000)
logging.getLogger("setools.defaultquery").removeHandler(self.handler)
def setupUi(self):
self.load_ui("defaultquery.ui")
# set up results
self.table_results_model = DefaultTableModel(self)
self.sort_proxy = QSortFilterProxyModel(self)
self.sort_proxy.setSourceModel(self.table_results_model)
self.table_results.setModel(self.sort_proxy)
self.table_results.sortByColumn(1, Qt.AscendingOrder)
# populate class list
self.class_model = SEToolsListModel(self)
self.class_model.item_list = sorted(self.policy.classes())
self.tclass.setModel(self.class_model)
# set up processing thread
self.thread = QThread()
self.worker = QueryResultsUpdater(self.query, self.table_results_model)
self.worker.moveToThread(self.thread)
self.worker.raw_line.connect(self.raw_results.appendPlainText)
self.worker.finished.connect(self.update_complete)
self.worker.finished.connect(self.thread.quit)
self.thread.started.connect(self.worker.update)
# create a "busy, please wait" dialog
self.busy = QProgressDialog(self)
self.busy.setModal(True)
self.busy.setRange(0, 0)
self.busy.setMinimumDuration(0)
self.busy.canceled.connect(self.thread.requestInterruption)
self.busy.reset()
# update busy dialog from query INFO logs
self.handler = LogHandlerToSignal()
self.handler.message.connect(self.busy.setLabelText)
logging.getLogger("setools.defaultquery").addHandler(self.handler)
# Ensure settings are consistent with the initial .ui state
self.default_range_2.setEnabled(self.default_range.isChecked())
self.notes.setHidden(not self.notes_expander.isChecked())
# connect signals
self.default_range.toggled.connect(self.default_range_2.setEnabled)
self.clear_ruletypes.clicked.connect(self.clear_all_ruletypes)
self.all_ruletypes.clicked.connect(self.set_all_ruletypes)
self.tclass.selectionModel().selectionChanged.connect(self.set_tclass)
self.invert_class.clicked.connect(self.invert_tclass_selection)
self.buttonBox.clicked.connect(self.run)
#
# Ruletype criteria
#
def _set_ruletypes(self, value):
self.default_user.setChecked(value)
self.default_role.setChecked(value)
self.default_type.setChecked(value)
self.default_range.setChecked(value)
def set_all_ruletypes(self):
self._set_ruletypes(True)
def clear_all_ruletypes(self):
self._set_ruletypes(False)
#
# Class criteria
#
def set_tclass(self):
selected_classes = []
for index in self.tclass.selectionModel().selectedIndexes():
selected_classes.append(self.class_model.data(index, Qt.UserRole))
self.query.tclass = selected_classes
def invert_tclass_selection(self):
invert_list_selection(self.tclass.selectionModel())
#
# Results runner
#
def run(self, button):
# right now there is only one button.
rule_types = []
for mode in [self.default_user, self.default_role, self.default_type, self.default_range]:
if mode.isChecked():
rule_types.append(mode.objectName())
self.query.ruletype = rule_types
self.query.default = self.default_2.currentData(Qt.DisplayRole)
if self.default_range_2.isEnabled():
self.query.default_range = self.default_range_2.currentData(Qt.DisplayRole)
else:
self.query.default_range = None
# start processing
self.busy.setLabelText("Processing query...")
self.busy.show()
self.raw_results.clear()
self.thread.start()
def update_complete(self, count):
self.log.info("{0} default(s) found.".format(count))
# update sizes/location of result displays
if not self.busy.wasCanceled():
self.busy.setLabelText("Resizing the result table's columns; GUI may be unresponsive")
self.busy.repaint()
self.table_results.resizeColumnsToContents()
if not self.busy.wasCanceled():
self.busy.setLabelText("Resizing the result table's rows; GUI may be unresponsive")
self.busy.repaint()
self.table_results.resizeRowsToContents()
if not self.busy.wasCanceled():
self.busy.setLabelText("Moving the raw result to top; GUI may be unresponsive")
self.busy.repaint()
self.raw_results.moveCursor(QTextCursor.Start)
self.busy.reset()

View File

@ -0,0 +1,52 @@
# Copyright 2016, Tresys Technology, LLC
#
# This file is part of SETools.
#
# SETools is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 2.1 of
# the License, or (at your option) any later version.
#
# SETools 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with SETools. If not, see
# <http://www.gnu.org/licenses/>.
#
from collections import defaultdict
from PyQt5.QtCore import Qt
from .models import SEToolsTableModel
class DefaultTableModel(SEToolsTableModel):
"""Table-based model for default_*."""
headers = defaultdict(str, {0: "Rule Type", 1: "Class", 2: "Default", 3: "Default Range"})
def data(self, index, role):
if self.resultlist:
row = index.row()
col = index.column()
item = self.resultlist[row]
if role == Qt.DisplayRole:
if col == 0:
return item.ruletype
elif col == 1:
return str(item.tclass)
elif col == 2:
return str(item.default)
elif col == 3:
try:
return str(item.default_range)
except AttributeError:
pass
elif role == Qt.UserRole:
return item