📄 q3databrowser.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the Qt3Support module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "q3databrowser.h"#ifndef QT_NO_SQL_VIEW_WIDGETS#include "q3sqlform.h"#include "private/q3sqlmanager_p.h"#include "qsqlresult.h"class Q3DataBrowserPrivate{public: Q3DataBrowserPrivate() : boundaryCheck(true), readOnly(false) {} Q3SqlCursorManager cur; Q3SqlFormManager frm; Q3DataManager dat; bool boundaryCheck; bool readOnly;};/*! \class Q3DataBrowser qdatabrowser.h \brief The Q3DataBrowser class provides data manipulation and navigation for data entry forms. \compat A high-level API is provided for navigating through data records in a cursor, for inserting, updating and deleting records, and for refreshing data in the display. If you want a read-only form to present database data use Q3DataView; if you want a table-based presentation of your data use Q3DataTable. A Q3DataBrowser is used to associate a dataset with a form in much the same way as a Q3DataTable associates a dataset with a table. Once the data browser has been constructed it can be associated with a dataset with setSqlCursor(), and with a form with setForm(). Boundary checking, sorting and filtering can be set with setBoundaryChecking(), setSort() and setFilter(), respectively. The insertCurrent() function reads the fields from the default form into the default cursor and performs the insert. The updateCurrent() and deleteCurrent() functions perform similarly to update and delete the current record respectively. The user can be asked to confirm all edits with setConfirmEdits(). For more precise control use setConfirmInsert(), setConfirmUpdate(), setConfirmDelete() and setConfirmCancels(). Use setAutoEdit() to control the behavior of the form when the user edits a record and then navigates. The record set is navigated using first(), next(), prev(), last() and seek(). The form's display is updated with refresh(). When navigation takes place the firstRecordAvailable(), lastRecordAvailable(), nextRecordAvailable() and prevRecordAvailable() signals are emitted. When the cursor record is changed due to navigation the cursorChanged() signal is emitted. If you want finer control of the insert, update and delete processes then you can use the lower level functions to perform these operations as described below. The form is populated with data from the database with readFields(). If the user is allowed to edit, (see setReadOnly()), write the form's data back to the cursor's edit buffer with writeFields(). You can clear the values in the form with clearValues(). Editing is performed as follows: \list \i \e insert When the data browser enters insertion mode it emits the primeInsert() signal which you can connect to, for example to pre-populate fields. Call writeFields() to write the user's edits to the cursor's edit buffer then call insert() to insert the record into the database. The beforeInsert() signal is emitted just before the cursor's edit buffer is inserted into the database; connect to this for example, to populate fields such as an auto-generated primary key. \i \e update For updates the primeUpdate() signal is emitted when the data browser enters update mode. After calling writeFields() call update() to update the record and connect to the beforeUpdate() signal to manipulate the user's data before the update takes place. \i \e delete For deletion the primeDelete() signal is emitted when the data browser enters deletion mode. After calling writeFields() call del() to delete the record and connect to the beforeDelete() signal, for example to record an audit of the deleted record. \endlist*//*! \enum Q3DataBrowser::Boundary This enum describes where the data browser is positioned. \value Unknown the boundary cannot be determined (usually because there is no default cursor, or the default cursor is not active). \value None the browser is not positioned on a boundary, but it is positioned on a record somewhere in the middle. \value BeforeBeginning the browser is positioned before the first available record. \value Beginning the browser is positioned at the first record. \value End the browser is positioned at the last record. \value AfterEnd the browser is positioned after the last available record.*//*! Constructs a data browser which is a child of \a parent, with the name \a name and widget flags set to \a fl.*/Q3DataBrowser::Q3DataBrowser(QWidget *parent, const char *name, Qt::WindowFlags fl) : QWidget(parent, name, fl){ d = new Q3DataBrowserPrivate(); d->dat.setMode(QSql::Update);}/*! Destroys the object and frees any allocated resources.*/Q3DataBrowser::~Q3DataBrowser(){ delete d;}/*! Returns an enum indicating the boundary status of the browser. This is achieved by moving the default cursor and checking the position, however the current default form values will not be altered. After checking for the boundary, the cursor is moved back to its former position. See \l Q3DataBrowser::Boundary. \sa Boundary*/Q3DataBrowser::Boundary Q3DataBrowser::boundary(){ Q3SqlCursor* cur = d->cur.cursor(); if (!cur || !cur->isActive()) return Unknown; if (!cur->isValid()) { if (cur->at() == QSql::BeforeFirst) return BeforeBeginning; if (cur->at() == QSql::AfterLast) return AfterEnd; return Unknown; } if (cur->at() == 0) return Beginning; int currentAt = cur->at(); Boundary b = None; if (!cur->previous()) b = Beginning; else cur->seek(currentAt); if (b == None && !cur->next()) b = End; cur->seek(currentAt); return b;}/*! \property Q3DataBrowser::boundaryChecking \brief whether boundary checking is active When boundary checking is active (the default), signals are emitted indicating the current position of the default cursor. \sa boundary()*/void Q3DataBrowser::setBoundaryChecking(bool active){ d->boundaryCheck = active;}bool Q3DataBrowser::boundaryChecking() const{ return d->boundaryCheck;}/*! \property Q3DataBrowser::sort \brief the data browser's sort The data browser's sort affects the order in which records are viewed in the browser. Call refresh() to apply the new sort. When retrieving the sort property, a string list is returned in the form 'fieldname order', e.g. 'id ASC', 'surname DESC'. There is no default sort. Note that if you want to iterate over the list, you should iterate over a copy, e.g. \code QStringList list = myDataBrowser.sort(); QStringList::Iterator it = list.begin(); while(it != list.end()) { myProcessing(*it); ++it; } \endcode*/void Q3DataBrowser::setSort(const QStringList& sort){ d->cur.setSort(sort);}/*! \overload Sets the data browser's sort to the QSqlIndex \a sort. To apply the new sort, use refresh().*/void Q3DataBrowser::setSort(const QSqlIndex& sort){ d->cur.setSort(sort);}QStringList Q3DataBrowser::sort() const{ return d->cur.sort();}/*! \property Q3DataBrowser::filter \brief the data browser's filter The filter applies to the data shown in the browser. Call refresh() to apply the new filter. A filter is a string containing a SQL WHERE clause without the WHERE keyword, e.g. "id>1000", "name LIKE 'A%'", etc. There is no default filter. \sa sort()*/void Q3DataBrowser::setFilter(const QString& filter){ d->cur.setFilter(filter);}QString Q3DataBrowser::filter() const{ return d->cur.filter();}/*! Sets the default cursor used by the data browser to \a cursor. If \a autoDelete is true (the default is false), the data browser takes ownership of the \a cursor pointer, which will be deleted when the browser is destroyed, or when setSqlCursor() is called again. To activate the \a cursor use refresh(). The cursor's edit buffer is used in the default form to browse and edit records. \sa sqlCursor() form() setForm()*/void Q3DataBrowser::setSqlCursor(Q3SqlCursor* cursor, bool autoDelete){ if (!cursor) return; d->cur.setCursor(cursor, autoDelete); d->frm.setRecord(cursor->editBuffer()); if (cursor->isReadOnly()) setReadOnly(true);}/*! Returns the default cursor used for navigation, or 0 if there is no default cursor. \sa setSqlCursor()*/Q3SqlCursor* Q3DataBrowser::sqlCursor() const{ return d->cur.cursor();}/*! Sets the browser's default form to \a form. The cursor and all navigation and data manipulation functions that the browser provides become available to the \a form.*/void Q3DataBrowser::setForm(Q3SqlForm* form){ d->frm.setForm(form);}/*! Returns the data browser's default form or 0 if no form has been set.*/Q3SqlForm* Q3DataBrowser::form(){ return d->frm.form();}/*! \property Q3DataBrowser::readOnly \brief whether the browser is read-only The default is false, i.e. data can be edited. If the data browser is read-only, no database edits will be allowed.*/void Q3DataBrowser::setReadOnly(bool active){ d->readOnly = active;}bool Q3DataBrowser::isReadOnly() const{ return d->readOnly;}void Q3DataBrowser::setConfirmEdits(bool confirm){ d->dat.setConfirmEdits(confirm);}/*! \property Q3DataBrowser::confirmInsert \brief whether the data browser confirms insertions If this property is true, the browser confirms insertions, otherwise insertions happen immediately. \sa confirmCancels() confirmEdits() confirmUpdate() confirmDelete() confirmEdit()*/void Q3DataBrowser::setConfirmInsert(bool confirm){ d->dat.setConfirmInsert(confirm);}/*! \property Q3DataBrowser::confirmUpdate \brief whether the browser confirms updates If this property is true, the browser confirms updates, otherwise updates happen immediately. \sa confirmCancels() confirmEdits() confirmInsert() confirmDelete() confirmEdit()*/void Q3DataBrowser::setConfirmUpdate(bool confirm){ d->dat.setConfirmUpdate(confirm);}/*! \property Q3DataBrowser::confirmDelete \brief whether the browser confirms deletions If this property is true, the browser confirms deletions, otherwise deletions happen immediately. \sa confirmCancels() confirmEdits() confirmUpdate() confirmInsert() confirmEdit()*/void Q3DataBrowser::setConfirmDelete(bool confirm){ d->dat.setConfirmDelete(confirm);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -