def splitProfileBelow(self, bed):
     if bed is None:
         return
     if QMessageBox.question(QApplication.activeWindow(),
                             QApplication.translate('profile column item', 'Split?'),
                             QApplication.translate('profile column item', 
                                                    QString('Split below bed %1?').arg(bed.number)),
                             QMessageBox.Yes | QMessageBox.No) != QMessageBox.Yes:
         return
     try:
         newProfile = Profile(self.profile.project,
                              None,
                              unicode(QString('Bottom of %1').arg(bed.profile.name)),
                              '',
                              0, self.profile.startHeightLengthUnit,
                              self.profile.scale,
                              self.profile.bigMarksDistanceValue,
                              self.profile.bigMarksDistanceLengthUnit,
                              self.profile.smallMarksDistanceValue,
                              self.profile.smallMarksDistanceLengthUnit,
                              self.profile.colsInLegend)
         self.getSession().add(newProfile)
         for c in self.profile.columns:
             self.getSession().add(ColumnInProfile(None, newProfile, c.profileColumn, c.position, c.width))
         b = self.profile.beds[-1]
         while b.number < bed.number:
             b.profile = newProfile
             b = self.profile.beds[-1]
         self.getSession().commit()
         self.drawBeds()
     except IntegrityError, e:
         dlg = DatabaseExceptionDialog(QApplication.activeWindow(), e)
         dlg.exec_()
         QApplication.instance().db.session.rollback()
 def mergeWithBelow(self, bed):
     below = self.profile.bedBelow(bed)
     if below is None:
         QMessageBox.information(QApplication.activeWindow(),
                                 QApplication.translate('profile column item', 'No Item'),
                                 QApplication.translate('profile column item', 'No Bed Below'))
         return
     if below.lengthUnit != bed.lengthUnit:
         QMessageBox.error(QApplication.activeWindow(),
                           QApplication.translate('profile column item', 'Error'),
                           QApplication.translate('profile column item', 'Beds must have same length unit.'))
         return
     if QMessageBox.question(QApplication.activeWindow(),
                             QApplication.translate('profile column item', 'Merge?'),
                             QApplication.translate('profile column item', 
                                                    QString('Merge %1 and %2?').arg(bed.number).arg(below.number)),
                             QMessageBox.Yes | QMessageBox.No) != QMessageBox.Yes:
         return
     try:
         self.profile.mergeBeds(below, bed)
         self.getSession().delete(below)
         self.getSession().commit();
         self.getSession().expire(self.profile)
         self.drawBeds()
     except IntegrityError, e:
         dlg = DatabaseExceptionDialog(QApplication.activeWindow(), e)
         dlg.exec_()
         QApplication.instance().db.session.rollback()
 def onDeleteRequest(self, idx):
     itm = self.itemFromIndex(idx)
     data = itm.data
     try:
         self.getSession().delete(data)
         self.getSession().commit();
         self.reload()
     except IntegrityError, e:
         dlg = DatabaseExceptionDialog(QApplication.activeWindow(), e)
         dlg.exec_()
         QApplication.instance().db.session.rollback()
 def save(self):
     if not self.validate():
         self.showFailedValidation()
         return
     try:
         if not self.entity.hasId():
             QApplication.instance().db.session.add(self.entity)
         QApplication.instance().db.session.commit()
         return True
     except IntegrityError, e:
         dlg = DatabaseExceptionDialog(QApplication.activeWindow(), e)
         dlg.exec_()
         QApplication.instance().db.session.rollback()
 def deleteBed(self, bed):
     if QMessageBox.warning(QApplication.activeWindow(),
                            QCoreApplication.translate('Profile Column Item', 
                                                       "Delete?"),
                            QCoreApplication.translate('Profile Column Item', 
                                                       "Delete Bed %1?").arg(bed.number),
                            QMessageBox.Yes | QMessageBox.No) != QMessageBox.Yes:
         return
     try:
         self.getSession().delete(bed)
         self.getSession().expire(self.profile)
         self.getSession().commit();
         self.drawBeds()
     except IntegrityError, e:
         dlg = DatabaseExceptionDialog(QApplication.activeWindow(), e)
         dlg.exec_()
         QApplication.instance().db.session.rollback()
 def createBedBelow(self, bed):
     if bed is None:
         return
     nmbr = bed.number - 1
     try:
         for b in reversed(self.profile.beds):
             if b.number < nmbr:
                 b.number -= 1
         bed = Bed(self.profile, None, 0, None, nmbr)
         self.editBed(bed)
         self.getSession().commit();
         self.getSession().expire(self.profile)
         self.drawBeds()
     except IntegrityError, e:
         dlg = DatabaseExceptionDialog(QApplication.activeWindow(), e)
         dlg.exec_()
         QApplication.instance().db.session.rollback()
 def moveDown(self, bed):
     below = self.profile.bedBelow(bed)
     if below is None:
         QMessageBox.information(QApplication.activeWindow(),
                                 QApplication.translate('profile column item', 'No Item'),
                                 QApplication.translate('profile column item', 'No Bed Below'))
         return
     try:
         belowNum = below.number
         myNum = bed.number
         below.number = myNum
         bed.number = belowNum
         self.getSession().commit();
         self.getSession().expire(self.profile)
         self.drawBeds()
     except IntegrityError, e:
         dlg = DatabaseExceptionDialog(QApplication.activeWindow(), e)
         dlg.exec_()
         QApplication.instance().db.session.rollback()
    def insertTemplateData(self):
        self.setupSvgFiles()
        d = dict()
        d['Bed Number Column'] = ProfileColumn(None, 'Bed Number', '', 'BedNumberHeader', 'BedNumberBedField')
        d['Bed Height Column'] = ProfileColumn(None, 'Bed Height', '', 'BedHeightHeader', 'BedHeightBedField')
        d['Bedding Type Column'] = ProfileColumn(None, 'Bedding Type', '', 'BeddingTypeHeader', 'BeddingTypeBedField')
        d['Outcrop Type Column'] = ProfileColumn(None, 'Outcrop Type', '', 'OutcropTypeHeader', 'OutcropTypeBedField')
        d['Lithology Column'] = ProfileColumn(None, 'Lithology', '', 'LithologyHeader', 'LithologyBedField')
        d['Fossils Column'] = ProfileColumn(None, 'Fossils', '', 'FossilHeader', 'FossilBedField')
        d['Sediment Structures'] = ProfileColumn(None, 'Sediment Structures', '', 'SedimentStructureHeader', 'SedimentStructureBedField')
        d['Custom Symbols'] = ProfileColumn(None, 'Custom Symbols', '', 'CustomSymbolHeader', 'CustomSymbolBedField')
        d['Lithological Unit Column'] = ProfileColumn(None, 'Lithological Unit', '', 'LithologicalUnitHeader', 'LithologicalUnitBedField')
        d['Tectonic Unit Column'] = ProfileColumn(None, 'Tectonic Unit', '', 'TectonicUnitHeader', 'TectonicUnitBedField')
        d['Stratigraphic Unit Column'] = ProfileColumn(None, 'Stratigraphic Unit', '', 'StratigraphicUnitHeader', 'StratigraphicUnitBedField')
        d['Facies Column'] = ProfileColumn(None, 'Facies', '', 'FaciesHeader', 'FaciesBedField')
        d['Colors Column'] = ProfileColumn(None, 'Color', '', 'ColorHeader', 'ColorBedField')
        d['Grain Size Column'] = ProfileColumn(None, 'Grain Size', '', 'GrainSizeHeader', 'GrainSizeBedField')

        d['um'] = LengthUnit(None, 1, unicode(self.tr('um')))
        d['mm'] = LengthUnit(None, 1000, unicode(self.tr('mm')))
        d['cm'] = LengthUnit(None, 10000, unicode(self.tr('cm')))
        d['dm'] = LengthUnit(None, 100000, unicode(self.tr('dm')))
        d['m'] = LengthUnit(None, 1000000, unicode(self.tr('m')))
        d['Krumbein1963'] = GrainSizeType(None, unicode('Krumbein, 1963'),
                                          unicode('W.C. Krumbein and L.L. Sloss(1963): Stratigraphy and Sedimentation, 2nd edition. Freeman, San Francisco'))
        d['Folk1964'] = GrainSizeType(None, unicode('Folk, 1964 (Wentworth Scale)'),
                                      unicode('Folk, 1964: A Review of Grain-Size Parameters. Sedimentology 6:2:73-93'))
        d['Dunham1962'] = GrainSizeType(None, unicode('Dunhame, 1962'),
                                        unicode('Dunhame, 1962: Classification of carbonate rocks according to depositional texture, in Ham, W.E. ed., Classification of carbonate rocks: Amierican Association of Petroleum Geologists Memoir 1, p. 108-121'))
        d['colloid'] = GrainSize(d['Folk1964'], None, unicode('Colloid'), '', None, None, 1, d['um'], 6, unicode('coll'))
        d['clay'] = GrainSize(d['Folk1964'], None, unicode('Clay'), '', 1, d['um'], 4, d['um'], 12, unicode('clay'))
        d['silt'] = GrainSize(d['Folk1964'], None, unicode('Silt'), '', 4, d['um'], 62, d['um'], 18, unicode('silt'))
        d['vfs'] = GrainSize(d['Folk1964'], None, unicode('Very Fine Sand'), '', 62, d['um'], 125, d['um'], 24, unicode('vfs'))
        d['fs'] = GrainSize(d['Folk1964'], None, unicode('Fine Sand'), '', 125, d['um'], 250, d['um'], 30, unicode('fs'))
        d['mfs'] = GrainSize(d['Folk1964'], None, unicode('Medium Sand'), '', 250, d['um'], 500, d['um'], 36, unicode('ms'))
        d['cs'] = GrainSize(d['Folk1964'], None, unicode('Coarse Sand'), '', 500, d['um'], 1, d['mm'], 42, unicode('cs'))
        d['vcs'] = GrainSize(d['Folk1964'], None, unicode('Very Coarse Sand'), '', 1, d['mm'], 2, d['mm'], 48, unicode('vcs'))
        d['granule'] = GrainSize(d['Folk1964'], None, unicode('Granule'), '', 2, d['mm'], 4, d['mm'], 54, unicode('gr'))
        d['vfp'] = GrainSize(d['Folk1964'], None, unicode('Very Fine Pebble'), '', 4, d['mm'], 8, d['mm'], 60, unicode('vfp'))
        d['fp'] = GrainSize(d['Folk1964'], None, unicode('Fine Pebble'), '', 8, d['mm'], 16, d['mm'], 66, unicode('fp'))
        d['p'] = GrainSize(d['Folk1964'], None, unicode('Pebble'), '', 16, d['mm'], 32, d['mm'], 72, unicode('p'))
        d['cp'] = GrainSize(d['Folk1964'], None, unicode('Coarse Pebble'), '', 32, d['mm'], 64, d['mm'], 78, unicode('cp'))
        d['cobble'] = GrainSize(d['Folk1964'], None, unicode('Cobble'), '', 64, d['mm'], 256, d['mm'], 84, unicode('cobble'))
        d['boulder'] = GrainSize(d['Folk1964'], None, unicode('Boulder'), '', 256, d['mm'], None, None, 96, unicode('boulder'))
        d['mudstone'] = GrainSize(d['Dunham1962'], None, unicode('Mudstone'), '', None, None, None, None, 16, unicode('mud'))
        d['wackestone'] = GrainSize(d['Dunham1962'], None, unicode('Wackestone'), '', None, None, None, None, 32, unicode('wacke'))
        d['packstone'] = GrainSize(d['Dunham1962'], None, unicode('Packstone'), '', None, None, None, None, 48, unicode('pack'))
        d['grainstone'] = GrainSize(d['Dunham1962'], None, unicode('Grainstone'), '', None, None, None, None, 64, unicode('grain'))
        d['boundstone'] = GrainSize(d['Dunham1962'], None, unicode('Boundstone'), '', None, None, None, None, 80, unicode('bound'))
        d['crystalline'] = GrainSize(d['Dunham1962'], None, unicode('Crystalline'), '', None, None, None, None, 96, unicode('cryst'))

        d['Test project'] = Project(None, unicode('Test Project'))

        for f in self.beddingTypeFiles:
            fn = QString("../artwork/bedding_types/%1").arg(f)
            d[fn] = SVGItem(None, unicode(fn), '', self.loadSvgFileContent(fn), unicode(fn))
            d['lithology %s' % fn] = BeddingType(d['Test project'], None, self.toItemName(f), d[fn], '', None)
        for f in self.boundaryTypeFiles:
            fn = QString("../artwork/boundary_types/%1").arg(f)
            d[fn] = SVGItem(None, unicode(fn), '', self.loadSvgFileContent(fn), unicode(fn))
            d['boundary type %s' % fn] = BoundaryType(d['Test project'], None, self.toItemName(f), d[fn], '', None)
        for f in self.colorFiles:
            fn = QString("../artwork/colors/%1").arg(f)
            d[fn] = SVGItem(None, unicode(fn), '', self.loadSvgFileContent(fn), unicode(fn))
            d['color %s' % fn] = Color(d['Test project'], None, self.toItemName(f), d[fn], '', None)
        for f in self.customSymbolFiles:
            fn = QString("../artwork/custom_symbols/%1").arg(f)
            d[fn] = SVGItem(None, unicode(fn), '', self.loadSvgFileContent(fn), unicode(fn))
            d['custom symbol %s' % fn] = CustomSymbol(d['Test project'], None, self.toItemName(f), d[fn], '', None)
        for f in self.faciesFiles:
            fn = QString("../artwork/facies/%1").arg(f)
            d[fn] = SVGItem(None, unicode(fn), '', self.loadSvgFileContent(fn), unicode(fn))
            d['facies %s' % fn] = Facies(d['Test project'], None, self.toItemName(f), d[fn], '', None)
        for f in self.fossilFiles:
            fn = QString("../artwork/fossils/%1").arg(f)
            d[fn] = SVGItem(None, unicode(fn), '', self.loadSvgFileContent(fn), unicode(fn))
            d['fossil %s' % fn] = Fossil(d['Test project'], None, self.toItemName(f), d[fn], '', None)
        for f in self.lithologyFiles:
            fn = QString("../artwork/lithological_patterns/%1").arg(f)
            d[fn] = SVGItem(None, unicode(fn), '', self.loadSvgFileContent(fn), unicode(fn))
            d['lithology %s' % fn] = Lithology(d['Test project'], None, self.toItemName(f), d[fn], '', None)
        for f in self.outcropTypeFiles:
            fn = QString("../artwork/outcrop_types/%1").arg(f)
            d[fn] = SVGItem(None, unicode(fn), '', self.loadSvgFileContent(fn), unicode(fn))
            d['outcrop type %s' % fn] = OutcropType(d['Test project'], None, self.toItemName(f), d[fn], '', None)
        for f in self.sedimentStructureFiles:
            fn = QString("../artwork/sediment_structures/%1").arg(f)
            d[fn] = SVGItem(None, unicode(fn), '', self.loadSvgFileContent(fn), unicode(fn))
            d['sediment structure %s' % fn] = SedimentStructure(d['Test project'], None, self.toItemName(f), d[fn], '', None)
        
        d['Limestone Mudstone'] = Lithology(d['Test project'], None, unicode('Limestone Mudstone'), None, unicode(''), d['mudstone'])
        d['red'] = Color(d['Test project'], None, unicode('Red'), None, unicode(''))
        d['massive'] = BeddingType(d['Test project'], None, unicode('Massive'), None, unicode(''))
        d['slump'] = SedimentStructure(d['Test project'], None, unicode('Slump'), None, unicode(''))
        d['snail'] = Fossil(d['Test project'], None, unicode('Snail'), None, unicode(''))
        d['sampling'] = CustomSymbol(d['Test project'], None, unicode('Sampling Point'), None, unicode(''))
        d['sharp planar'] = BoundaryType(d['Test project'], None, unicode('Sharp Planar'), None, unicode(''))
        d['oc1'] = PointOfInterest(d['Test project'], None, unicode('Outcrop 1'), None, unicode(''))
        d['Profile 1'] = Profile(d['Test project'], None, unicode('Profile 1'), unicode(''), 0, d['m'],
                                 10,
                                 1, d['m'], 10, d['cm'])
        d['folk in 1'] = GrainSizeTypeInProfile(None,  d['Folk1964'], d['Profile 1'])
        d['dunham in 1'] = GrainSizeTypeInProfile(None, d['Dunham1962'], d['Profile 1'])

        d['Number in profile 1'] = ColumnInProfile(None, d['Profile 1'], d['Bed Number Column'], 0, 50)
        d['Height in profile 1'] = ColumnInProfile(None, d['Profile 1'], d['Bed Height Column'], 1, 50)
        d['bedding type in profile 1'] = ColumnInProfile(None, d['Profile 1'], d['Bedding Type Column'], 2, 50)
        d['outcrop type in profile 1'] = ColumnInProfile(None, d['Profile 1'], d['Outcrop Type Column'], 3, 50)
        d['lithology in profile 1'] = ColumnInProfile(None, d['Profile 1'], d['Lithology Column'], 4, 50)
        d['fossils in profile 1'] = ColumnInProfile(None, d['Profile 1'], d['Fossils Column'], 5, 50)
        d['sediment structures in profile 1'] = ColumnInProfile(None, d['Profile 1'], d['Sediment Structures'], 6, 50)
        d['custom symbols in profile 1'] = ColumnInProfile(None, d['Profile 1'], d['Custom Symbols'], 7, 50)
        d['lithological units in profile 1'] = ColumnInProfile(None, d['Profile 1'], d['Lithological Unit Column'], 8, 50)
        d['tectonic units in profile 1'] = ColumnInProfile(None, d['Profile 1'], d['Tectonic Unit Column'], 9, 50)
        d['stratigraphic units in profile 1'] = ColumnInProfile(None, d['Profile 1'], d['Stratigraphic Unit Column'], 10, 50)
        d['facies in profile 1'] = ColumnInProfile(None, d['Profile 1'], d['Facies Column'], 11, 50)
        d['colors in profile 1'] = ColumnInProfile(None, d['Profile 1'], d['Colors Column'], 12, 50)
        d['grain sizes in profile 1'] = ColumnInProfile(None, d['Profile 1'], d['Grain Size Column'], 13, 200)

        bedNumber = 1
        while bedNumber < 100:
            n = 'Bed %s' % bedNumber
            d[n] = Bed(d['Profile 1'], None, 15, d['cm'], bedNumber)
            d[n].updateName()
            LithologyInBed(d[n], None,  d['Limestone Mudstone'], 0, 100, unicode('limestone in %s' %n))
            bedNumber += 1
        try: 
            s = self.db.session
            for k, v in d.iteritems():
                s.add(v)
            s.commit()
        except SQLError, e:
            dlg = DatabaseExceptionDialog(self.activeWindow(), e)
            dlg.exec_()
            try:
                s.rollback()
            except SQLError, re:
                dlg = DatabaseExceptionDialog(self.activeWindow(), re)
                dlg.exec_()
            self.showFailedValidation()
            return
        try:
            if not self.entity.hasId():
                QApplication.instance().db.session.add(self.entity)
            QApplication.instance().db.session.commit()
            return True
        except IntegrityError, e:
            dlg = DatabaseExceptionDialog(QApplication.activeWindow(), e)
            dlg.exec_()
            QApplication.instance().db.session.rollback()
        except ConcurrentModificationError, e:
            dlg = DatabaseExceptionDialog(QApplication.activeWindow(), e)
            dlg.exec_()
            QApplication.instance().db.session.rollback()
        except ProgrammingError, e:
            dlg = DatabaseExceptionDialog(QApplication.activeWindow(), e)
            dlg.exec_()
            QApplication.instance().db.session.rollback()
        return False
    def accept(self):
        if not self.validate():
            self.showFailedValidation()
            return
        if self.save():
            self.done(QDialog.Accepted)
    def reject(self):
        if self.entity.hasId():
            QApplication.instance().db.session.refresh(self.entity)
        self.done(QDialog.Rejected)