// Draws a line from (x1, y1) to (x2, y2) and sets the current pen position to (x2, y2).
void drawLine( int x1, int y1, int x2, int y2 );
// Draws the text starting at point (x, y) and using the default KPlus font.
void drawText( int x, int y, const char* text );
// Draws the text starting at point (x, y) and using the font "font".
void drawText( int x, int y, const char* text, const QFont& font );
void drawText( int x, int y, int width, int height, const char* text);
void drawText( int x, int y, int width, int height, const char* text,
int flags);
void drawText( int x, int y, int width, int height, const char* text,
const QFont& font );
void drawText( int x, int y, int width, int height, const char* text,
int flags, const QFont& font );
// Draws the rectangle defined by the point (x, y)
// and the dimensions w and h.
void drawRect( int x, int y, int w, int h, const QPen& = QPen() );
void drawRect( int x, int y, int w, int h, int color ) ;
// Draws the selection rectangle directly onto the viewport
// This must only be called from the Repaint_Notify!
void drawSelectionRect( int x, int y, int w, int h, int color );
// Draws the pixmap image into the rectangle defined by the point (x, y)
// and the dimensions w and h.
// If needed, the pixmap is scaled to fit the rectangle.
void drawPixmap( int x, int y, int w, int h, const QPixmap& image );
// Displaying the elements...
void setRepaintProc(RepaintProc_t repaint);
void KCanvas::drawLine( int x1, int y1, int x2, int y2 )
{
QPainter painter(&d->pixmap_);
painter.drawLine(x1, y1, x2, y2);
}
void KCanvas::drawText( int x, int y, const char* text )
{
const QFont kplusFont = QApplication::font();
this->drawText(x, y, text, kplusFont);
}
void KCanvas::drawText( int x, int y, const char* text, const QFont& font )
{
QString qText(text);
QPainter painter(&d->pixmap_);
painter.setFont(font);
painter.drawText(x, y, qText);
}
void KCanvas::drawText( int x, int y, int width, int height, const char* text)
{
const QFont kplusFont = QApplication::font();
this->drawText(x, y, width, height, text, kplusFont);
}
void KCanvas::drawText( int x, int y, int width, int height, const char* text,
int flags)
{
const QFont kplusFont = QApplication::font();
this->drawText(x, y, width, height, text, flags, kplusFont);
}
void KCanvas::drawText( int x, int y, int width, int height,
const char* text, const QFont& font )
{
int flags = Qt::TextShowMnemonic | Qt::AlignVCenter | Qt::AlignLeft;
this->drawText(x, y, width, height, text, flags, font);
}
void KCanvas::drawText( int x, int y, int width, int height,
const char* text, int flags, const QFont& font )
{
QString qText(text);
QPainter painter(&d->pixmap_);
painter.setFont(font);
painter.drawText(x, y, width, height, flags, qText);
}
void KCanvas::drawRect( int x, int y, int w, int h, const QPen& pen )
{
QPainter painter(&d->pixmap_);
painter.setPen(pen);
painter.drawRect(x, y, w, h);
}
void KCanvas::drawRect( int x, int y, int w, int h, int color )
{
QPainter painter(&d->pixmap_);
const int selectionColor = color ^ 0xFFFFFF;
painter.setPen(selectionColor);
painter.drawRect(x, y, w, h);
}
void KCanvas::drawSelectionRect( int x, int y, int w, int h, int color )
{
QPainter painter(viewport());
// Emulate XOR on white background
const int selectionColor = color ^ 0xFFFFFF;
painter.setPen(selectionColor);
painter.drawRect(x, y, w, h);
}
void KCanvas::drawPixmap( int x, int y, int w, int h,
const QPixmap& image )
{
const QRect rect = QRect(x, y, w, h);
QPainter painter(&d->pixmap_);
painter.drawPixmap(rect, image);
}
// This is the "paintEvent" for the canvas.
void KCanvas::drawContents( QPainter*, int, int, int, int )
{
// All the drawing methods have drawn into d->pixmap_.
// Now blit it to the screen
QPainter painter(viewport());
painter.translate(-contentsX(), -contentsY());
// TODO optimization: use the clipping arguments
painter.drawPixmap(0, 0, d->pixmap_);
// This callback is now used for painting -additional- stuff, like selection.
painter.translate(contentsX(), contentsY());
if( repaintCB_ )
(*repaintCB_)( this );
}
void KCanvas::drawContents( QPainter* )
{
// Empty - Just to avoid compiler warning
}
void KCanvas::resizeContents(int width, int height)
{
// Resize both the canvas contents, and the pixmap for it
Q3ScrollView::resizeContents(width, height);
QFont KCanvas::selectFont(int bold, int /size/) const
{
QFont font = this->font();
font.setBold(bold);
//font.setPointSize(size);
return font;
}
Please reimplement this class for pure Qt4 ( you can do this wit assumption that KFrame inherits from QWidget and KEvent from QEvent, KItem could be ommitted)
Assistant
To reimplement the KCanvas class for pure Qt4 without dependencies on Qt3Support or Q3ScrollView, while assuming KFrame inherits from QWidget and KEvent inherits from QEvent, I will follow a few key steps:
Replace Q3ScrollView with QScrollArea which is the Qt4 equivalent for scrollable widgets.
Remove any references to deprecated classes and update method calls to use Qt4 functions.
Make sure to adapt the drawing and event filtering mechanisms to use the appropriate Qt4 classes and methods.
Ensure that the logic for handling events and painting is correctly transferred to the KCanvas implementation.
Here's the new implementation of the KCanvas class for Qt4:
cpp
#ifndef KCANVAS_H
#define KCANVAS_H
#include <QScrollArea>
#include <QPen>
#include <QPainter>
#include <QPixmap>
#include <QMouseEvent>
#include <QApplication>
#include <QVBoxLayout>
#include "KFrame.h" // Assuming KFrame is defined somewhere to inherit QWidget
#include "KEvent.h" // Assuming KEvent is derived from QEvent
class KCanvas : public QScrollArea
{
Q_OBJECT
public:
// Repaint callback
typedef void (*RepaintProc_t)(KCanvas*);
// Event callback
typedef void (*EventProc_t)(KCanvas*, KEvent*);
explicit KCanvas(QWidget* parent = nullptr);
~KCanvas();
void drawLine(int x1, int y1, int x2, int y2);
void drawText(int x, int y, const char* text);
void drawText(int x, int y, const char* text, const QFont& font);
void drawPixmap(int x, int y, int w, int h, const QPixmap& image);
void drawRect(int x, int y, int w, int h, const QPen& pen);
void setRepaintProc(RepaintProc_t repaint);
void setEventProc(EventProc_t event);
void removeContents();
void clear();
protected:
void paintEvent(QPaintEvent* event) override;
bool eventFilter(QObject* object, QEvent* event) override;
private:
RepaintProc_t repaintCB_;
EventProc_t eventCB_;
QPixmap pixmap_;
void init();
};
#endif // KCANVAS_H
User
I have class defined as : #ifndef KCANVAS_H
#define KCANVAS_H
// Draws a line from (x1, y1) to (x2, y2) and sets the current pen position to (x2, y2).
void drawLine( int x1, int y1, int x2, int y2 );
// Draws the text starting at point (x, y) and using the default KPlus font.
void drawText( int x, int y, const char* text );
// Draws the text starting at point (x, y) and using the font "font".
void drawText( int x, int y, const char* text, const QFont& font );
void drawText( int x, int y, int width, int height, const char* text);
void drawText( int x, int y, int width, int height, const char* text,
int flags);
void drawText( int x, int y, int width, int height, const char* text,
const QFont& font );
void drawText( int x, int y, int width, int height, const char* text,
int flags, const QFont& font );
// Draws the rectangle defined by the point (x, y)
// and the dimensions w and h.
void drawRect( int x, int y, int w, int h, const QPen& = QPen() );
void drawRect( int x, int y, int w, int h, int color ) ;
// Draws the selection rectangle directly onto the viewport
// This must only be called from the Repaint_Notify!
void drawSelectionRect( int x, int y, int w, int h, int color );
// Draws the pixmap image into the rectangle defined by the point (x, y)
// and the dimensions w and h.
// If needed, the pixmap is scaled to fit the rectangle.
void drawPixmap( int x, int y, int w, int h, const QPixmap& image );
// Displaying the elements...
void setRepaintProc(RepaintProc_t repaint);
private:
// Not implemented - Just to avoid compiler warning
void drawContents( QPainter* ) override;
Assistant
To reimplement the KCanvas class for pure Qt4 while maintaining as much of the original interface as possible, we'll need to make several changes. The main strategies will be to replace the Q3ScrollView with QScrollArea, modify the drawing and event handling mechanisms, and clean up the usage of private data.
KCanvas.h
Here's the updated header file for KCanvas. It retains the same public methods and signatures wherever possible, while adapting to Qt4's QScrollArea and its event handling.
cpp
#ifndef KCANVAS_H
#define KCANVAS_H
#include <QScrollArea>
#include <QPen>
#include <QPixmap>
#include <QMouseEvent>
#include <QLayout>
#include <QPainter>
#include <QFont>
#include <QApplication>
class KEvent; // Forward declaration
class KCanvas : public QScrollArea
{
Q_OBJECT
public:
// Repaint callback
typedef void (*RepaintProc_t)(KCanvas*);
// Event callback
typedef void (*EventProc_t)(KCanvas*, KEvent*);
explicit KCanvas(QWidget* parent = nullptr);
~KCanvas() override;
void drawLine(int x1, int y1, int x2, int y2);
void drawText(int x, int y, const char* text);
void drawText(int x, int y, const char* text, const QFont& font);
void drawPixmap(int x, int y, int w, int h, const QPixmap& image);
void drawRect(int x, int y, int w, int h, const QPen& pen);
void drawRect(int x, int y, int w, int h, int color);
void drawSelectionRect(int x, int y, int w, int h, int color);
void setRepaintProc(RepaintProc_t repaint);
void setEventProc(EventProc_t event);
void removeContents();
void clear();
// Resize contents
void resizeContents(int width, int height);
void setWidth(int width);
void setColors(int fg, int bg);
QFont findFont() const;
QFont findBoldFont() const;
QFont selectFont(int bold, int size) const;
protected:
void paintEvent(QPaintEvent* event) override;
bool eventFilter(QObject* object, QEvent* event) override;
private:
RepaintProc_t repaintCB_;
EventProc_t eventCB_;
QPixmap pixmap_;
void initializePixmap();
};
#endif // KCANVAS_H
User
we have this method in class Q3ScrollView* KPanel::scrollView()
{
if ( ! scrollView_ )
{
QWidget* parent = (QWidget*)this->parent();
code
scrollView_ = new Q3ScrollView(parent);
int width = parent->width();
int height = parent->height();
if (width > this->width())
width = this->width();
if (height > this->height())
height = this->height();
scrollView_->setGeometry(
this->x(),
this->y(),
width,
height);
scrollView_->addChild(this);
// Attention, maintenant le positionnement de this
// se fait par rapport a la scrollview.
}
return scrollView_;
} but isntead Q3ScrollView we want use QScrollArea. How this method shoukld look to have same behaviour
Assistant
To rewrite the KPanel::scrollView() method that originally uses Q3ScrollView to now use QScrollArea, we need to make several adjustments.
Key Considerations:
QScrollArea Setup: Unlike Q3ScrollView, QScrollArea doesn't directly manage child widgets in an addChild manner. Instead, you set a widget to be scrollable by calling the setWidget() method.
Widget Resizing: QScrollArea automatically handles resizing and allows you to set what widget it displays, you often need to wrap your existing widget (in this case, this) in another widget to manage the layout.
Geometry Management: We need to adjust for the fact that the scrollArea doesn't manage the position of this directly, so we'll have to set the geometry of it appropriately.
Here is a possible implementation of the scrollView function updated for QScrollArea:
cpp
QScrollArea* KPanel::scrollView()
{
if (!scrollView_)
{
QWidget* parent = this->parentWidget();
// Create a new QScrollArea
scrollView_ = new QScrollArea(parent);
// Set the same width and height constraints
int width = this->width() < parent->width() ? this->width() : parent->width();
int height = this->height() < parent->height() ? this->height() : parent->height();
// Set the geometry of the QScrollArea
scrollView_->setGeometry(this->x(), this->y(), width, height);
// Create a new widget to contain "this"
QWidget *container = new QWidget(scrollView_);
QVBoxLayout *layout = new QVBoxLayout(container);
layout->addWidget(this);
container->setLayout(layout);
// Set the container as the widget for the QScrollArea
scrollView_->setWidget(container);
scrollView_->setWidgetResizable(true); // Allow the widget to resize with the scroll area
// Note: Now the positioning of "this" is managed relative to the scroll area.
}
return scrollView_;
}
User
I had this method in class : void KFrame::makeShrinkable(bool isLocked, bool isAfakeDialogReplacementCase)
{
if (!qscrollview_ )
{
setMinimumSize(QSize(0,0));
if (statusbar_)
delete statusbar_;
code
statusbar_ = new QStatusBar(this, "frame_footer");
statusbar_->setAutoFillBackground(true);
qscrollview_ = new Q3ScrollView(this, "scroll_view");
qscrollview_->setHScrollBarMode(Q3ScrollView::Auto);
qscrollview_->setVScrollBarMode(Q3ScrollView::Auto);
qscrollview_->setResizePolicy(Q3ScrollView::Manual);
// We want the Q3Scrollview to be totally transparent...
// Replacing it by a QScrollArea would be better, but much more expensive.
qscrollview_->viewport()->setBackgroundColor(this->palette().color(this->backgroundRole()));
QList<QWidget*> widgets = KGlobal::findDirectChildren<QWidget *>(this);
Q_FOREACH(QWidget* obj, widgets)
{
if (!obj->inherits("Q3ScrollView") && !obj->inherits("QMenu")
&& !obj->inherits("Q3Menu") && !obj->inherits("KFrame"))
{
obj->setParent(qscrollview_->viewport());
}
}
if (!layout())
{
QLayout* layout = new QVBoxLayout(this);
layout->setContentsMargins(0,0,0,0);
layout->setSpacing(0);
}
if(qscrollview_)
layout()->add(qscrollview_);
if (statusbar_)
layout()->add(statusbar_);
}
//Do not uncomment this statment because we need the Frame to be no resizable
//setMinimumSize(QSize(0,0)); now class should use QScrollArea from Qt4 how I should reimplement this method to have exactly same behaviour
Assistant
To reimplement the KFrame::makeShrinkable method using QScrollArea from Qt4 while maintaining the same behavior as the original with Q3ScrollView, we need to consider the following changes:
Initialize a QScrollArea instead of Q3ScrollView: We will replace Q3ScrollView with QScrollArea.
Set Scroll Bar Modes: Unlike Q3ScrollView, which has functions to set scroll bar modes, QScrollArea automatically shows scroll bars when necessary. We won't explicitly set scroll bar modes.
Background Color Handling: The viewport in QScrollArea can be styled using a palette similarly.
Children Management: The logic for setting the parent of children widgets will remain largely the same.
Layout Adjustments: We need to ensure the layout adds the QScrollArea and the status bar correctly.
Here’s how to implement KFrame::makeShrinkable:
KFrame::makeShrinkable Implementation
cpp
User
I have this class which is using Q3ScrollView : // FILE: Libs/libKQT/KFrame.cpp
if (QEvent::KeyPress == ev->type())
{
QKeyEvent* k = (QKeyEvent*) ev;
if (k->state() & Qt::ControlModifier)
{
KFrame* theDialog = qobject_cast<KFrame*>(objDlg);
if (k->key() == Qt::Key_R)
{
// Attempting to issue the clear methods on panels
const QList<KPanel *> panelsLst = theDialog->queryPanels();
Q_FOREACH( KPanel* curPanel, panelsLst )
{
// for each found panel...
if (KToolBar * toolbar = curPanel->getToolbar())
{
int TOOL_ERASER = 1 << 6; // duplicated Panel_tools_t
if (KButton * clearButton = toolbar->find(TOOL_ERASER)) {
((KButton::callback_type)(intptr_t)(clearButton->getCallback()))(clearButton, nullptr);
}
}
}
}
else if (k->key() == Qt::Key_Q)
{
if (getenv("KONDOR_QA_MODE"))
{
QAFrameDumper(theDialog).dump();
//return true;
}
}
else if (k->key() == Qt::Key_W)
{
KEWFrameDumper(theDialog).dump();
}
}
}
return false;
Assistant
To reimplement the KFrame class so that it uses QScrollArea instead of Q3ScrollView, we need to replace the instances of Q3ScrollView with QScrollArea, and adjust methods accordingly to account for the differences between the two classes.
Here's how the refactored version of KFrame would look. I've made the necessary adjustments while attempting to retain the original class structure and functionality.
KFrame.h (Header File)
cpp
#ifndef _KFRAME_H_
#define _KFRAME_H_
#include <QtGui/qwidget.h>
#include <QtGui/qstatusbar.h>
#include <QtGui/qscrollarea.h> // Include QScrollArea
#include "KItem.h"
#include <string>
class KEvent;
class KFrame;
class KPanelItem;
class KPanel;
class KGUIComponentHeader;
class KFrame : public QWidget, public KItem
{
Q_OBJECT
Q_INTERFACES(KItem)
Q_PROPERTY(bool resizable READ isResizable WRITE setResizable)
Q_PROPERTY(bool statusbar READ hasStatusBar WRITE setStatusBar)
typedef void (*callback_type)(KFrame* frame);
typedef int (*close_if_type)(KFrame* frame);
public:
static const int SIZE_POS_EVENT = 65000;
explicit KFrame(QWidget* parent = nullptr, const char* name = nullptr);
~KFrame() override;
// Rest of the original declarations...
QScrollArea* qScrollView() const; // Update return type
void makeShrinkable(bool isLocked = false, bool isAfakeDialogReplacementCase = false);
void makeShrinkable(); // For convenience
protected:
void showEvent(QShowEvent*) override;
void resizeEvent(QResizeEvent* qRE) override;
private:
// Original member variables...
QScrollArea* qscrollarea_; // Change from Q3ScrollView to QScrollArea
QStatusBar* statusbar_;
};
#endif // _KFRAME_H_
// Initialization method, called in the ctor
virtual void init( );
Assistant
To update the KFrame class to use QScrollArea instead of Q3ScrollView, and to ensure that the widgets within KFrame are correctly parented to the QScrollArea, you need to make several changes to both the header and implementation file.
The key steps will be to:
Change the declaration from Q3ScrollView* to QScrollArea*.
Adjust the makeShrinkable method to properly add all widgets to the QScrollArea.
Ensure that the QScrollArea is added to the layout, with the scroll area containing the contents of the KFrame.
Implement any necessary changes to maintain the existing functionality, ensuring widgets are correctly managed and interactions are preserved.
KFrame.h (Header File)
Change the included header for Q3ScrollView to QScrollArea and update any relevant declarations:
cpp
#ifndef _KFRAME_H_
#define _KFRAME_H_
#include <QtGui/qwidget.h>
#include <QtGui/qstatusbar.h>
#include <QtGui/qscrollarea.h> // Updated to include QScrollArea
#include "KItem.h"
#include <string>
class KEvent;
class KFrame;
class KPanelItem;
class KPanel;
class KGUIComponentHeader;
typedef int ( *Notify_func )( KFrame* popup, KEvent *event, int arg, int type);
class KFrame : public QWidget, public KItem
{
Q_OBJECT
Q_INTERFACES(KItem)
Q_PROPERTY(bool resizable READ isResizable WRITE setResizable)
Q_PROPERTY(bool statusbar READ hasStatusBar WRITE setStatusBar)
typedef void (*callback_type)( KFrame* frame );
typedef int (*close_if_type)(KFrame* frame);
public:
static const int SIZE_POS_EVENT = 65000;
explicit KFrame( QWidget* parent = nullptr, const char* name = nullptr );
~KFrame() override;
std::string toString(int indent = 0) const;
int getTableId() const { return tableid_; }
void setTableId(int tableid) { tableid_ = tableid; }
// Statusbar property
bool hasStatusBar() const;
void setStatusBar(bool);
QStatusBar *statusBar() const;
QScrollArea* qScrollView() const; // Change the return type to QScrollArea*
void makeShrinkable();
void makeShrinkable(bool isLocked, bool isAfakeDialogReplacementCase);
// Other member functions...
private:
// Original member variables...
QScrollArea* qscrollarea_; // Change from Q3ScrollView to QScrollArea
// Other variables...
};
#endif // _KFRAME_H_
void KCanvas::drawLine( int x1, int y1, int x2, int y2 )
{
QPainter painter(&d->pixmap_);
painter.drawLine(x1, y1, x2, y2);
}
void KCanvas::drawText( int x, int y, const char* text )
{
const QFont kplusFont = QApplication::font();
this->drawText(x, y, text, kplusFont);
}
void KCanvas::drawText( int x, int y, const char* text, const QFont& font )
{
QString qText(text);
QPainter painter(&d->pixmap_);
painter.setFont(font);
painter.drawText(x, y, qText);
}
void KCanvas::drawText( int x, int y, int width, int height, const char* text)
{
const QFont kplusFont = QApplication::font();
this->drawText(x, y, width, height, text, kplusFont);
}
void KCanvas::drawText( int x, int y, int width, int height, const char* text,
int flags)
{
const QFont kplusFont = QApplication::font();
this->drawText(x, y, width, height, text, flags, kplusFont);
}
void KCanvas::drawText( int x, int y, int width, int height,
const char* text, const QFont& font )
{
int flags = Qt::TextShowMnemonic | Qt::AlignVCenter | Qt::AlignLeft;
this->drawText(x, y, width, height, text, flags, font);
}
void KCanvas::drawText( int x, int y, int width, int height,
const char* text, int flags, const QFont& font )
{
QString qText(text);
QPainter painter(&d->pixmap_);
painter.setFont(font);
painter.drawText(x, y, width, height, flags, qText);
}
void KCanvas::drawRect( int x, int y, int w, int h, const QPen& pen )
{
QPainter painter(&d->pixmap_);
painter.setPen(pen);
painter.drawRect(x, y, w, h);
}
void KCanvas::drawRect( int x, int y, int w, int h, int color )
{
QPainter painter(&d->pixmap_);
const int selectionColor = color ^ 0xFFFFFF;
painter.setPen(selectionColor);
painter.drawRect(x, y, w, h);
}
void KCanvas::drawSelectionRect( int x, int y, int w, int h, int color )
{
QPainter painter(viewport());
// Emulate XOR on white background
const int selectionColor = color ^ 0xFFFFFF;
painter.setPen(selectionColor);
painter.drawRect(x, y, w, h);
}
void KCanvas::drawPixmap( int x, int y, int w, int h,
const QPixmap& image )
{
const QRect rect = QRect(x, y, w, h);
QPainter painter(&d->pixmap_);
painter.drawPixmap(rect, image);
}
// This is the "paintEvent" for the canvas.
void KCanvas::drawContents( QPainter*, int, int, int, int )
{
// All the drawing methods have drawn into d->pixmap_.
// Now blit it to the screen
QPainter painter(viewport());
painter.translate(-contentsX(), -contentsY());
// TODO optimization: use the clipping arguments
painter.drawPixmap(0, 0, d->pixmap_);
// This callback is now used for painting -additional- stuff, like selection.
painter.translate(contentsX(), contentsY());
if( repaintCB_ )
(*repaintCB_)( this );
}
void KCanvas::drawContents( QPainter* )
{
// Empty - Just to avoid compiler warning
}
void KCanvas::resizeContents(int width, int height)
{
// Resize both the canvas contents, and the pixmap for it
Q3ScrollView::resizeContents(width, height);
QFont KCanvas::selectFont(int bold, int /size/) const
{
QFont font = this->font();
font.setBold(bold);
//font.setPointSize(size);
return font;
}.
Please reimplement this class for pure Qt4 ( you can do this wit assumption that KFrame inherits from QWidget and KEvent from QEvent, KItem could be ommitted). Do the best to keep old interface
KCanvas.cpp
Here's the implementation file with the necessary adjustments for Qt4 functionality.
cpp
#include "KCanvas.h"
#include "KEvent.h"
#include "KFrame.h" // Include only if needed
KCanvas::KCanvas(QWidget* parent)
: QScrollArea(parent), repaintCB_(nullptr), eventCB_(nullptr)
{
initializePixmap();
setWidgetResizable(true);
setWidget(new QWidget); // Set a widget for drawing
widget()->setAttribute(Qt::WA_OpaquePaintEvent);
widget()->installEventFilter(this);
}
KCanvas::~KCanvas()
{
}
void KCanvas::initializePixmap()
{
pixmap_ = QPixmap(size());
pixmap_.fill(Qt::white);
}
void KCanvas::drawLine(int x1, int y1, int x2, int y2)
{
QPainter painter(&pixmap_);
painter.drawLine(x1, y1, x2, y2);
viewport()->update(); // Refresh the viewport
}
void KCanvas::drawText(int x, int y, const char* text)
{
const QFont kplusFont = QApplication::font();
drawText(x, y, text, kplusFont);
}
void KCanvas::drawText(int x, int y, const char* text, const QFont& font)
{
QString qText(text);
QPainter painter(&pixmap_);
painter.setFont(font);
painter.drawText(x, y, qText);
viewport()->update(); // Refresh the viewport
}
void KCanvas::drawPixmap(int x, int y, int w, int h, const QPixmap& image)
{
QPainter painter(&pixmap_);
painter.drawPixmap(x, y, w, h, image);
viewport()->update(); // Refresh the viewport
}
void KCanvas::drawRect(int x, int y, int w, int h, const QPen& pen)
{
QPainter painter(&pixmap_);
painter.setPen(pen);
painter.drawRect(x, y, w, h);
viewport()->update(); // Refresh the viewport
}
void KCanvas::drawRect(int x, int y, int w, int h, int color)
{
QPainter painter(&pixmap_);
painter.setPen(QColor(color));
painter.drawRect(x, y, w, h);
viewport()->update(); // Refresh the viewport
}
void KCanvas::drawSelectionRect(int x, int y, int w, int h, int color)
{
QPainter painter(viewport());
painter.setPen(QColor(color));
painter.drawRect(x, y, w, h);
}
void KCanvas::setRepaintProc(RepaintProc_t repaint)
{
repaintCB_ = repaint;
}
void KCanvas::setEventProc(EventProc_t event)
{
if (eventCB_)
widget()->removeEventFilter(this);
eventCB_ = event;
if (eventCB_)
widget()->installEventFilter(this);
}
bool KCanvas::eventFilter(QObject* object, QEvent* event)
{
if (object == widget() && (event->type() == QEvent::MouseButtonPress ||
event->type() == QEvent::MouseButtonRelease ||
event->type() == QEvent::MouseMove))
{
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
KEvent::Action action = KEvent::ACTION_SELECT;
if (event->type() == QEvent::MouseMove)
{
if (mouseEvent->buttons() & Qt::LeftButton)
{
action = KEvent::LOC_DRAG;
}
else
{
return QScrollArea::eventFilter(object, event);
}
}
KEvent kevent(action,
(event->type() == QEvent::MouseButtonPress ?
KEvent::MOUSE_ACTION_DOWN :
event->type() == QEvent::MouseButtonRelease ?
KEvent::MOUSE_ACTION_UP :
KEvent::MOUSE_ACTION_DOUBLECLICK),
(mouseEvent->button() == Qt::LeftButton ?
KEvent::MOUSE_BUTTON_LEFT :
mouseEvent->button() == Qt::RightButton ?
KEvent::MOUSE_BUTTON_RIGHT :
KEvent::MOUSE_BUTTON_MIDDLE),
mouseEvent->x(), mouseEvent->y());
if (eventCB_)
eventCB_(this, &kevent);
}
return QScrollArea::eventFilter(object, event);
}
void KCanvas::clear()
{
pixmap_.fill(Qt::white);
viewport()->update(); // Refresh the viewport
}
void KCanvas::removeContents()
{
for (QLayoutItem* item : widget()->layout()->children())
{
delete item->widget();
}
}
void KCanvas::resizeContents(int width, int height)
{
QSize wantedSize(width, height);
if (pixmap_.size() != wantedSize)
{
pixmap_ = QPixmap(wantedSize);
pixmap_.fill(Qt::white);
}
}
void KCanvas::setWidth(int width)
{
this->resize((width > 0) ? width : parentWidget()->width(), height());
}
void KCanvas::setColors(int fg, int bg)
{
QPalette palette = viewport()->palette();
palette.setColor(QPalette::Foreground, QColor(fg));
palette.setColor(QPalette::Background, QColor(bg));
viewport()->setPalette(palette);
}
QFont KCanvas::findFont() const
{
return font();
}
QFont KCanvas::findBoldFont() const
{
QFont font = this->font();
font.setBold(true);
return font;
}
QFont KCanvas::selectFont(int bold, int /*size*/) const
{
QFont font = this->font();
font.setBold(bold);
return font;
}
void KCanvas::paintEvent(QPaintEvent* event)
{
QPainter painter(viewport());
painter.drawPixmap(0, 0, pixmap_);
if(repaintCB_)
repaintCB_(this);
}
Key Changes and Their Explanation:
Base Class Replacement: Q3ScrollView is replaced with QScrollArea, enabling better support in Qt4.
Event Handling: The eventFilter method is adapted to better handle mouse events and dispatch them properly using the KEvent structure.
Drawing Logic: Drawing operations are done on a QPixmap which is then drawn in the paintEvent, allowing KCanvas to be a self-painting widget.
Useful Methods Implemented: Methods like clear, setWidth, and resizeContents are implemented, providing functionality that mirrors the original design, but using Qt4 paradigms.
Rendering Logic in paintEvent: It pulls from pixmap_ and calls the repaintCB_, allowing callback functions for additional rendering after the base drawing is completed.
With these changes, KCanvas should integrate well into a Qt4 application while maintaining a similar interface and functionality to the original version. Adjust any other functions or specifics based on your application's architecture as needed!
Explanation of Changes:
Parent Widget Check: We still check for the parent widget to attach the scroll area correctly.
Creating Scroll Area: We create a new QScrollArea instead of Q3ScrollView.
Container Widget: We wrap this in a new QWidget as QScrollArea expects a single widget to manage, and since this itself might contain complex layouts or multiple child widgets, it is good practice to encapsulate it.
Layout Management: We apply a QVBoxLayout (or QHBoxLayout, depending on the desired arrangement) to the container widget for proper layout handling.
Geometry Settings: We set the geometry of the QScrollArea and allow it to resize its contained widget by calling setWidgetResizable(true).
This restructure will maintain the intended functionality while leveraging the properties of QScrollArea. Additionally, ensure your class has the scrollView_ pointer properly defined and manage the memory appropriately, avoiding memory leaks or dangling pointers.
void KFrame::makeShrinkable(bool isLocked, bool isFakeDialogReplacementCase)
{
if (!qscrollarea_)
{
setMinimumSize(QSize(0, 0));
if (statusbar_)
delete statusbar_;
statusbar_ = new QStatusBar(this);
statusbar_->setAutoFillBackground(true);
// Initialize the QScrollArea instead of Q3ScrollView
qscrollarea_ = new QScrollArea(this);
qscrollarea_->setFrameShape(QFrame::NoFrame); // Keep it visually consistent
qscrollarea_->setWidgetResizable(true); // Allow the contained widget to resize
// Set the background color of the viewport to match this widget's background
qscrollarea_->viewport()->setAutoFillBackground(true);
qscrollarea_->viewport()->setPalette(this->palette());
// Get all direct children of this widget to update their parent
QList<QWidget*> widgets = KGlobal::findDirectChildren<QWidget*>(this);
Q_FOREACH(QWidget* obj, widgets)
{
if (!obj->inherits("QScrollArea") && !obj->inherits("QMenu")
&& !obj->inherits("Q3Menu") && !obj->inherits("KFrame"))
{
obj->setParent(qscrollarea_->viewport());
}
}
// Ensure there is a layout for the main frame
if (!layout())
{
QVBoxLayout* layout = new QVBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0);
}
// Add the QScrollArea and status bar to the layout
if (qscrollarea_)
layout()->addWidget(qscrollarea_); // Use addWidget for QScrollArea
if (statusbar_)
layout()->addWidget(statusbar_); // Use addWidget for QStatusBar
}
// Note: No need to setMinimumSize again as it already set previously
}
Explanation of Changes:
Using QScrollArea: The qscrollview_ variable is replaced with qscrollarea_, which is now an instance of QScrollArea instead of Q3ScrollView.
Removing Scrollbar Mode Settings: QScrollArea automatically handles scroll bars, so there's no need to set them explicitly. We don’t call any scrollbar mode methods.
Setting Background Colors: QScrollArea's viewport can be styled using setAutoFillBackground(true) and setting the palette accordingly.
Children Management: Widget re-parenting is unchanged; we still loop through direct children and re-parent them to the viewport() of the QScrollArea.
Layout Management: New layout is created only if it’s not already set up.
Using addWidget(): Use addWidget() for adding the QScrollArea and QStatusBar to the layout instead of add(), which is more appropriate for widget containers.
Following these adjustments ensures that the KFrame::makeShrinkable method maintains its intended functionality while working with QScrollArea in a Qt4 environment.
KFrame::KFrame(QWidget* parent, const char* name)
: QWidget(nullptr, Qt::Window )
, popup(this)
, closeCB_(nullptr)
, closeIfCB_(nullptr)
, interposeFunc_(nullptr)
, statusbar_(nullptr)
, tableid_(0)
, pushpin_(false)
, id_(0) // TODO: remove
, busy_(0)
, frameSetupReport_(nullptr)
, modified_(false)
, qscrollview_(nullptr)
, startX_(0)
, startY_(0)
, adjustSizeFreezed_(false)
, adjustSizeRequested_(false)
, setBusyLock_(false)
{
// By default we take the font provided by the QApplication.
// This font is set in Xview_init function.
// If this was to be overridden in the UI file or by code,
// this would be a development mistake and a business justification
// would have to be given, otherwise...
setObjectName(name ? name : "unnamed" );
code
setInstance(this);
//Mandatory for Report Dialogs at least
setKeyData( PFRAME, (char*)parent );
// Ensure the background color is set
KQApplication* kapp = dynamic_cast<KQApplication*>(qApp);
if(kapp)
{
QPalette palette;
palette.setColor(this->backgroundRole(), QColor(kapp->dlgBackgroundColor_));
this->setPalette(palette);
}
// Plug a spy to trap Ctrl+R events
// DON'T CHANGE THE FILTERS ORDER
ClearRequestFilter* myFilter = new ClearRequestFilter();
this->installEventFilter(myFilter);
this->installEventFilter( new Size_Position_Filter( this ));
this->installEventFilter( new StateFilter(this));
setWindowIcon(QPixmap(TurazLogo)); // to ensure that all KFrames will display the right logo even if setWindowFlags is called on it...
// In popup mode, we want to ensure the current dialog
// will be displayed on top of its parent to increase usability.
// In case of no parent, the screen of the active dialog is used.
if (parent)
{
this->move(parent->x(), parent->y());
}
else
{
const int K_DIALOG_OFFSET = 10;
QWidget* referenceDlg = qApp->activeWindow(); // Plan B
if (nullptr == referenceDlg) // Plan C
{
referenceDlg = qApp->mainWidget();
}
QDesktopWidget* deskTop = QApplication::desktop();
QRect activeScreenGeo = deskTop->screenGeometry(referenceDlg);
this->move(activeScreenGeo.x() + K_DIALOG_OFFSET, activeScreenGeo.y() + K_DIALOG_OFFSET);
}
QWidget::setAttribute( Qt::WA_WState_Hidden );
/** High dpi - force instrument rescale **/
this->setProperty("HIGH_DPI_DO_RESCALE", true);
void KFrame::init() {
// Now is the right time to find all our children and
// force them to find out what is their instance.
// Avoids random crashes later on when said children are
// reparented (and their instance is supposed to be the one
// at construction time).
QWidgetList widgets = KGlobal::findDirectChildren<QWidget *>(this);
if (!statusbar_ ) {
statusbar_ = new QStatusBar(this, "frame_footer");
statusbar_->setAutoFillBackground(true);
statusbar_->setSizeGripEnabled(isResizable());
statusbar_->adjustSize();
// Qt3-like behavior: activate layout.
if (layout()) {
// Save hidden state and make all children visible
QList<bool> hiddenStates;
for(int i = 0; i< widgets.count(); ++i) {
hiddenStates << widgets.at(i)->isHidden();
widgets.at(i)->setHidden(false);
}
// Activate the layout so that we've meaningful
// size and positions to make the sorting on.
// We do it here for compat with Qt3, which triggered it from adjustSize (in KDRT-70),
// but not if the statusbar already existed (KDRT-179).
// Restore hidden state
for(int i = 0; i< widgets.count(); ++i) {
widgets.at(i)->setHidden( hiddenStates.at(i) );
}
}
// Add the size of the status bar for fixed-size windows
if (!isResizable())
setFixedSize(width(), height() + statusbar_->height());
statusbar_->setMaximumHeight(statusbar_->height());
}
// Create a layout for resizable frames...
if (isResizable())
{
createLayout(widgets);
}
}
}
void KFrame::recomputeLayout()
{
if (layout())
{
return;
}
#ifdef DEBUG_WIDGET_SORTING
qDebug() << "About to sort the children of" << (void*)this << ":";
Q_FOREACH(QWidget* w, directWidgetsChild) {
qDebug() << " " << (void*)w << w->metaObject()->className() << w->objectName() << "y=" << w->y();
}
#endif
// Do the sorting
VerticalWidgetList sorted_widgets(directWidgetsChild);
sorted_widgets.sort();
#ifdef DEBUG_WIDGET_SORTING
// Reflect the layout-insertion order: statusbar is last
directWidgetsChild = sorted_widgets;
directWidgetsChild.removeAll(statusbar_);
directWidgetsChild.append(statusbar_);
QByteArray ba = qgetenv("WINDOW_DISPLAY_INFO");
if ( !ba.isEmpty() && (ba.toInt() == 1) )
widgetInfo( this );
}
// Duplicated code from Qt just to add the isTopLevel constraint
// This is because we don't want sub windows to be taken into account
// when performing a size adjustment!
QRect KFrame::childrenRect() const
{
QRect r(0, 0, 0, 0);
int statusBarHeight = 0;
if (statusbar_)
statusBarHeight = statusbar_->height();
if(qscrollview_)
{
QRect rect = this->childrenRect();
int svMargin = qscrollview_->frameWidth() *2 ;
QSize dlgSizeHint = QSize(rect.width() +svMargin, rect.height() +svMargin+ statusBarHeight);
QSize contentSizeHint =QSize( rect.width()-2*svMargin,rect.height()-2*svMargin);
setMinimumSize(QSize(0,0));
setMaximumSize(dlgSizeHint);
if (qscrollview_->verticalScrollBar()->isVisible() == false
&& qscrollview_->horizontalScrollBar()->isVisible() == false)
{
resize(dlgSizeHint);
}
qscrollview_->resizeContents(contentSizeHint.width(),contentSizeHint.height());
QVariant variant = property(Kprops_KustomWindowSize);
if (true == variant.isValid() && QVariant::Size == variant.type())
{
QSize size = variant.value<QSize>();
size.setHeight(std::max(size.height(), dlgSizeHint.height()));
size.setWidth(std::max(size.width(), dlgSizeHint.width()));
setMaximumHeight(size.height() + statusBarHeight);
setMaximumWidth(size.width());
if (contentSizeHint.width() < size.width() || contentSizeHint.height() < size.height())
{
qscrollview_->resize(contentSizeHint);
}
resize(size);
}
// workaround for Q3ScrollView bug, which calls ensureVisible without
// updating visibleWidth/visibleHeight first. (KDRT-162)
qscrollview_->updateScrollBars();
}
else
{
if (statusbar_ && !layout()) {
// We recompute the geometry of the frame
// without status bar, and then we add this later // at the very bottom of the frame!
statusbar_->hide();
QRect rect = this->childrenRect();
statusbar_->show();
statusbar_->setGeometry(0, rect.bottom(), width(), statusBarHeight);
resize(rect.width() + 2 * rect.x(), rect.height() + 2 * rect.y()
+ statusBarHeight);
}
else
{
/*
* Frame size should be adjusted only once against sizeHint
* If Size_Position_Filter have geometry stored it should be used
* rather than this mechanism.
*/
if ( !this->property("GEOMETRY_MANAGED").isValid() )
QWidget::adjustSize();
}
}
// Additional use case that occures when the KFrame itself
// is hosted into a QScrollView to fit into the physical screen!
if (qscrollview_)
{
// thism method calls showMaximized method from QWidget interface. According to Trolltech manual :
/*
Shows the widget maximized.
Calling this function only affects top-level widgets.
On X11, this function may not work properly with certain window managers.
See the Window Geometry documentation for an explanation.
*/
adjustScrollable(this);
}
}
bool KFrame::getBusy() {
return busy_;
}
void KFrame::setBusy(bool busy)
{
if(setBusyLock_)
{
/**
* The setBusy method is locked, because an
* event has not finished processing this method,
* and another event was generated in the Q_FOREACH
* loop below.
*/
return;
}
ScopedValueAssigner locked(setBusyLock_, true);
code
if (busy_ == busy)
return;
busy_ = busy;
QWidget *w = nullptr;
if (qscrollview_){
w = qscrollview_->viewport();
} else {
w = this;
}
// Disable all the panels of the widget, but not the widget itself,
// so that we can use the busy cursor (if we disable the top-level widget,
// we won't be able to use it)
QList<KPanel *> panels = KGlobal::findDirectChildren<KPanel *>(w);
Q_FOREACH(KPanel* child, panels) {
child->setBusy(busy);
}
QList<QWidget *> children = KGlobal::findDirectChildren<QWidget *>(w);
Q_FOREACH(QWidget* child, children) {
if (child->inherits("KFramelist")) // can't use qobject_cast, different lib
child->setEnabled(!busy);
}
setWaitCursor(this, busy);
// Reenable the status bar
if (busy) {
if (statusbar_)
statusbar_->setEnabled(true);
}
if (closeIfCB_) {
proceed = closeIfCB_(this);
} else if (closeCB_)
closeCB_(this);
if (1 == proceed)
{
QWidget::closeEvent(event);
}
else {
// in order to prevent the scrollable view
// that may contain the current frame, from being closed!
event->ignore();
}
}
KGUIComponentHeader* KFrame::getHeader() {
QList<QWidget *> list = qFindChildren<QWidget *>(this);
QListIterator<QWidget *> it( list );
/*
The printing mechanism has some "inconsistencies" when panels' items are printed.
When e.g. frame prints it's panels then each panel should embrace contents of controls by:
"[ Panel-Item PANEL_NAME [
...
postscript content written by controls
...
]]"
In the ideal situation this postscript code should be inserted by the same C++ function .
However the opening part of this script ("[ Panel-Item PANEL_NAME [")
is writen to the file by the first control which have any contents because frame
doesn't know if and when to open this tag.
Moreover the tag isn't closed by the panel item object (e.g. KTextEdit object) - only the frame knows if a control
has to print something and when to close the panel's tag after the last child with the use of "]]"
/
bool KFrame::print(FILE fd)
{
std::map< KPanel , std::vector<KPanelItem> > allItemsGroupByDirectParentPanel;
allItemsGroupByDirectParentPanel.clear();
code
//retrieve direct panels children
QWidget* frame = nullptr;
if(qscrollview_)
frame = qscrollview_->viewport();
else
frame = this;
const QList<KPanel*> panelsLst = KGlobal::findDirectChildren<KPanel *>(frame);
Q_FOREACH( KPanel* curPanel, panelsLst )
{
// for each found panel, we retrieve, recursively, its items.
const QList<KPanelItem*>* items = curPanel->queryItems();
if( items )
{
//group items by their panel parent only if the item is active (we don't have to print inactive field)
QListIterator<KPanelItem*> it(*items);
while (it.hasNext())
{
KPanelItem* curItem = it.next();
if (curItem->getPanel() == nullptr)
{
continue;
}
// Append all items in the list because there is no difference between ReadOnly and Disabled entries
std::vector<KPanelItem*> & itemsVector = allItemsGroupByDirectParentPanel[curItem->getPanel()];
itemsVector.push_back(curItem);
}
delete items;
}
}
std::vector<KPanel*> sortedPrintablePanels;
sortedPrintablePanels.clear();
for( std::map< KPanel* , std::vector<KPanelItem*> >::iterator itmap = allItemsGroupByDirectParentPanel.begin();
itmap != allItemsGroupByDirectParentPanel.end();
++itmap )
{
//check if panels containing items are printable (ie it is printable and all its parent panels too )
if( (*itmap).first->hasToBePrint() )
sortedPrintablePanels.push_back((*itmap).first);
}
if ( sortedPrintablePanels.empty() )
return false;
//sort printable panels by their absolute position in the frame
std::sort(sortedPrintablePanels.begin(), sortedPrintablePanels.end(), SortKPanelByPosition());
std::vector<KPanel*>::iterator itPanel = sortedPrintablePanels.begin();
//has something has been printed
bool hasPrinted = false;
for(; itPanel != sortedPrintablePanels.end(); ++itPanel)
{
std::map< KPanel* , std::vector<KPanelItem*> >::iterator itmap = allItemsGroupByDirectParentPanel.find(*itPanel);
if (itmap != allItemsGroupByDirectParentPanel.end())
{
std::sort((*itmap).second.begin(), (*itmap).second.end(), SortKPanelItemByPosition());
int nb = 0;
int op = FALSE;
std::vector<KPanelItem*>::iterator curItem = (*itmap).second.begin();
for(; curItem != (*itmap).second.end(); ++curItem)
{
// if curItem prints anything then panel's tag is opened by this object (see print PanelItem::print implementation)
(*curItem)->print(fd,nb, op);
}
// only if anything has been printed then the frame closes panel's tag
// for KLineEdit this is 0 though but op is 1
if (nb)
{
fprintf(fd," ]\n ] %% End of Panel Item %%\n");
}
if(nb || op)
hasPrinted = true;
}
}
const QList<KTextEdit*> textLst = KGlobal::findDirectChildren<KTextEdit *>(frame);
// print TextEdit content
Q_FOREACH( KTextEdit* curText, textLst )
{
int nb = 0;
int op = FALSE;
curText->print(fd, nb, op);
if (nb) // close control tag only if curText has printed anything
{
fprintf(fd," ]\n ] %% End of TextEdit Panel Item %%\n");
hasPrinted = true;
}
}
return hasPrinted;
}
void
KFrame::mouseMoveEvent(QMouseEvent* e)
{
if ((e->state() == Qt::MidButton)&& qscrollview_)
{
int offsetTempX = e->pos().x() - startX_;
int offsetTempY = e->pos().y() - startY_;
void KFrame::makeShrinkable(bool isLocked, bool isAfakeDialogReplacementCase)
{
if (!qscrollview_ )
{
setMinimumSize(QSize(0,0));
if (statusbar_)
delete statusbar_;
code
statusbar_ = new QStatusBar(this, "frame_footer");
statusbar_->setAutoFillBackground(true);
qscrollview_ = new Q3ScrollView(this, "scroll_view");
qscrollview_->setHScrollBarMode(Q3ScrollView::Auto);
qscrollview_->setVScrollBarMode(Q3ScrollView::Auto);
qscrollview_->setResizePolicy(Q3ScrollView::Manual);
// We want the Q3Scrollview to be totally transparent...
// Replacing it by a QScrollArea would be better, but much more expensive.
qscrollview_->viewport()->setBackgroundColor(this->palette().color(this->backgroundRole()));
QList<QWidget*> widgets = KGlobal::findDirectChildren<QWidget *>(this);
Q_FOREACH(QWidget* obj, widgets)
{
if (!obj->inherits("Q3ScrollView") && !obj->inherits("QMenu")
&& !obj->inherits("Q3Menu") && !obj->inherits("KFrame"))
{
obj->setParent(qscrollview_->viewport());
}
}
if (!layout())
{
QLayout* layout = new QVBoxLayout(this);
layout->setContentsMargins(0,0,0,0);
layout->setSpacing(0);
}
if(qscrollview_)
layout()->add(qscrollview_);
if (statusbar_)
layout()->add(statusbar_);
}
//Do not uncomment this statment because we need the Frame to be no resizable
//setMinimumSize(QSize(0,0));
}
void KFrame::restoreTabOrder() {
qDebug(" TAB ORDER NOT RESTORED for : %s class %s . Reimplement this method in ...ui.h file", ( name() ? name():"UNNAMED" ), className());
}
void KFrame::setModeCustomWindow(bool cwMode)
{
// if Custom Window mode
if (cwMode)
{
int fontSize = 8;
#ifdef WIN32
//37620: Help list from open report may not be readable with font "Terminal"
QString fontName("Courier New");
#else
QString fontName("Bitstream Vera Sans Mono");
fontSize = 7;
#endif
code
// create fixed width font
QFont fixedWidthFont(fontName, fontSize);
// apply font to frame
this->setFont(fixedWidthFont);
}
}
QFont KFrame::setFixedFont()
{
int fontSize = 8;
#ifdef WIN32
QString fontName("Courier New");
#else
QString fontName("Bitstream Vera Sans Mono");
fontSize = 7;
#endif
code
// create fixed width font
QFont fixedWidthFont(fontName, fontSize);
return fixedWidthFont;
if(this->qScrollView())
{
theOldParent = this->qScrollView()->viewport();
//carefull, it is called with KPanel in case of RK instrumentCriteriaChanged
KFrame* frame = qobject_cast<KFrame*>(parent);
if (frame && !parent->layout())
{
parent->makeShrinkable();
}
}
else
{
theOldParent = this;
}
QList<KPanel*> panels = KGlobal::findDirectChildren<KPanel *>(theOldParent);
Q_FOREACH(KPanel* obj, panels)
{
obj->reparent(parent,obj->pos(),true);
obj->setInstance(parent);
}
}
KFrame* KFrame::getByName(const char* name)
{
KFrame* childFrame = nullptr;
QWidgetList list = QApplication::allWidgets();
QWidget * w;
Q_FOREACH(w, list)
{
if (w->inherits("KFrame"))
{
if (!(strcmp(w->name(), name )))
{
childFrame = (KFrame* )w;
break;
}
}
}
return childFrame;
}
KFrame* KFrame::createFrameCMDFull(QWidget* owner,
int footer_flag,
int resize_flag,
int /pin_flag/,
int width,
int height,
void(* /event_proc/)(),
char* cd,
char* buffer
)
{
KFrame* frame = new KFrame(owner, buffer);
code
KPanel* panel = new KPanel(frame,"panel_cmd"); // Do not remove this statement. A panel named panel_cmd is expected in such KFrame.
Q_UNUSED(panel);
// Note: The order of the 3 following statements must be preserved!
frame->resize(width, height);
frame->setResizable(resize_flag);
frame->setStatusBar(footer_flag);
frame->setClientData(cd);
return frame;
}
KFrame* KFrame::createFrame(const char* label, int width, int height, QPixmap* image)
{
KFrame* frame = new KFrame;
frame->setResizable( false );
frame->setStatusBar( true );
frame->modify(label, width, height, image);
bool KFrame::skipNotifications() {
QVariant prop = qApp->property("DISABLE_NOTIFICATIONS");
if ( prop.isValid() )
return prop.toBool();
else
return false;
}
which is defined here // FILE: Libs/libKQT/KFrame.h
// Initialization method, called in the ctor
virtual void init( );
/**
* This virtual method should be reimplemented in all popup objects in ...ui.h files.
* In the body of the reimplemented method call to tabulationOrder(this) should
* be done.
*/
virtual void restoreTabOrder();
// Enables/disables the frame and eventually shows the hourglass cursor
void setBusy( bool busy );
bool getBusy( );
code
// For Interpose Function
void setInterposeFunc(Notify_func func)
{ interposeFunc_ = func; }
Notify_func getInterposeFunc()
{ return interposeFunc_; }
void closeEvent( QCloseEvent* ) override;
code
// To use for the reportManager
void setFrameSetupReport(KFrame *f){frameSetupReport_ = f;}
KFrame * getFrameSetupReport(){return frameSetupReport_;}
void setModified(bool f);
bool getModified();
// Duplicated code from Qt just to add the isTopLevel constraint
// This is because we don't want sub windows to be taken into account
// when performing a size adjustment!
QRect childrenRect() const;
void setWFlags(unsigned flag);
// Retreive K+ panel item by its name.
// 0 is return when not found.
KPanelItem* childPanelItem(const char* itemName);
/* Those two methods overrides QWidget and QObject */
/*!
@brief This method overrides parentWidget() from QWidget
code
It will return parent object passed to KFrame in the constructor.
This object will not allways be the same as passed to QWidget (ancestor of KFrame).
In the context of MDI the active workspace object is passed to the QWidget not the parent
from KFrame constructor. Parent object is stored in the map provided by
the second KFrame ancestor KItem by calling the :
<B>setKeyData ( PARENT_FRAME, (char*)parent );</B>
where PARENT_FRAME is defined in KFrame.c and parent is an address of
real ( Kondor+ ) parent passed to the constructor of KFrame and usually
used for retrive or set data on/from particular object. If user will want
to use a display parent passed to the QWidget in MDI mode he need to
explicitly call <B>obj->QWidget::parentWidget();</B> in other case the
object stored in KItem will be returned.
@param sameWindow - if true pointer to this object will be returned if there is
no parent otherwise return NULL
@return pointer to parent widget of QWidget type ( usualy it's Kondor+ ) parent to
obtain the data.
/*!
@brief This method overrides parent() from QObject.
code
It will return parent object passed to KFrame in the constructor. This object will not allways
be the same as passed to QWidget (ancestor of KFrame). In the context of
MDI the active workspace object is passed to the QWidget not the parent
from KFrame constructor. Parent object is stored in the map provided by
the second KFrame ancestor KItem by calling the :
<B>setKeyData ( PARENT_FRAME, (char*)parent );</B>
where PARENT_FRAME is defined in KFrame.c and parent is an address of
real ( Kondor+ ) parent passed to the constructor of KFrame and usually
used for retrive or set data on/from particular object. If user will want
to use a display parent passed to the QWidget in MDI mode he need to
explicitly call <B>obj->QWidget::parent();</B> in other case the
object stored in KItem will be returned.
@return pointer to parent widget of QObject type ( usualy it's Kondor+ ) parent to
obtain the data.
/
QObject logicalParent() const;
/*!
This method will set the parent widget in the context of KFrame intrface.
Please note that in context of Qt interface QWidget::parent will be still kept.
*/
void setLogicalParent(QWidget *parent);
/*!
@brief Generic print function for a frame. (write in fd, postscript script).
Prints all activate and non null items which are contained in shown panel.
It also prints out active and not shown tab widget.
code
@return true if something has been printed.
*/
bool print(FILE *fd);
/**
@brief lock the adjustSize fct, call releaseAdjustSize to unlock
@return true if the frame adjustSize is already lock
*/
bool freezeAdjustSize();
/**
@brief unlock adjustSize fct, if adjustSize fonction was called during the lock,
adjustSize is called here.
@param alreadyFreezed if true do nothing, must be the return value of freezeAdjustSize()
*/
void releaseAdjustSize(bool alreadyFreezed);
public:
void recomputeLayout(); //use with caution !!!
bool hasOwnTabOrder();
public:
/**
* @brief - Dump the window content with all widget/value couples.
* This function is used for automatic tests.
*/
std::string dumpContentAsXml() const;
private:
// Helper - Recursively sets/unsets the hourglass cursor on a widget
static void setWaitCursor( QWidget* widget, bool wait );
void showElement();
void createLayout(QWidgetList& widgets);
code
// Callback called when the window is closed
callback_type closeCB_;
close_if_type closeIfCB_;
// Interpose Function
Notify_func interposeFunc_;
// Statusbar
QStatusBar* statusbar_;
// Table id
int tableid_;
code
//Pushpin boolean
bool pushpin_;
// ID - TO REMOVE
int id_;
bool busy_;
code
// Use to report Manager
KFrame * frameSetupReport_;
bool modified_;
int startX_;
int startY_;
bool adjustSizeFreezed_;
bool adjustSizeRequested_;
bool setBusyLock_;
};
#endif // KFRAME_H
plase reimplement this class to use QSCrollArea instead of Q3ScrollView.
KFrame.cpp (Implementation File)
cpp
#include "KFrame.h"
#include "KGlobal.h"
// ... other includes
KFrame::KFrame(QWidget* parent, const char* name)
: QWidget(nullptr, Qt::Window),
// ... initialize other members
qscrollarea_(nullptr) // Initialize the new member variable
{
// Constructor content as before...
}
KFrame::~KFrame() {
delete qscrollarea_; // Change to delete the QScrollArea
delete statusbar_;
}
QScrollArea* KFrame::qScrollView() const {
return qscrollarea_; // Return the QScrollArea
}
void KFrame::makeShrinkable(bool isLocked, bool isAfakeDialogReplacementCase) {
if (!qscrollarea_) {
setMinimumSize(QSize(0, 0));
if (statusbar_)
delete statusbar_;
// Creating new QStatusBar
statusbar_ = new QStatusBar(this);
statusbar_->setAutoFillBackground(true);
// Initialize the QScrollArea
qscrollarea_ = new QScrollArea(this);
qscrollarea_->setFrameShape(QFrame::NoFrame); // No frame to keep it transparent
qscrollarea_->setWidgetResizable(true); // Allow contained widget resizing
// Set the background color to match this widget's background
qscrollarea_->viewport()->setAutoFillBackground(true);
qscrollarea_->viewport()->setPalette(this->palette());
// Reparenting child widgets to the viewport of the QScrollArea
QList<QWidget*> widgets = KGlobal::findDirectChildren<QWidget *>(this);
Q_FOREACH(QWidget* obj, widgets) {
if (!obj->inherits("QScrollArea") && !obj->inherits("QMenu") &&
!obj->inherits("KFrame")) {
obj->setParent(qscrollarea_->viewport());
}
}
// If no layout is set up, create one
if (!layout()) {
QVBoxLayout* layout = new QVBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0);
}
if (qscrollarea_)
layout()->addWidget(qscrollarea_); // Add the QScrollArea using addWidget
if (statusbar_)
layout()->addWidget(statusbar_); // Add the status bar as well
}
}
// Adjust other functions (e.g., resize, mouse events) accordingly to use QScrollArea
Explanation of Changes:
Change of ScrollView: Changed qscrollview_ from type Q3ScrollView* to QScrollArea*.
QScrollArea Initialization: Updated makeShrinkable to initialize QScrollArea with setWidgetResizable(true) and to use viewport()->setPalette() to apply background colors.
Reparenting Widgets: Adjusted the logic that reparents children to ensure they are correctly set as children of the new QScrollArea's viewport.
Updating Methods: Any additional methods that look for the scroll view (like qScrollView()) now reference the QScrollArea.
Memory Management: The destructor now deletes the QScrollArea instead of the old Q3ScrollView.
This updated implementation will allow KFrame to use QScrollArea instead of the outdated Q3ScrollView, maintaining the original behavior where feasible while adapting to the updated Qt4 usage. Adjustments should also be made in related methods to ensure full compatibility and correct functionality with the QScrollArea API.
/**
* This virtual method should be reimplemented in all popup objects in ...ui.h files.
* In the body of the reimplemented method call to tabulationOrder(this) should
* be done.
*/
virtual void restoreTabOrder();
// Enables/disables the frame and eventually shows the hourglass cursor
void setBusy( bool busy );
bool getBusy( );
code
// For Interpose Function
void setInterposeFunc(Notify_func func)
{ interposeFunc_ = func; }
Notify_func getInterposeFunc()
{ return interposeFunc_; }
void closeEvent( QCloseEvent* ) override;
code
// To use for the reportManager
void setFrameSetupReport(KFrame *f){frameSetupReport_ = f;}
KFrame * getFrameSetupReport(){return frameSetupReport_;}
void setModified(bool f);
bool getModified();
// Duplicated code from Qt just to add the isTopLevel constraint
// This is because we don't want sub windows to be taken into account
// when performing a size adjustment!
QRect childrenRect() const;
void setWFlags(unsigned flag);
// Retreive K+ panel item by its name.
// 0 is return when not found.
KPanelItem* childPanelItem(const char* itemName);
/* Those two methods overrides QWidget and QObject */
/*!
@brief This method overrides parentWidget() from QWidget
code
It will return parent object passed to KFrame in the constructor.
This object will not allways be the same as passed to QWidget (ancestor of KFrame).
In the context of MDI the active workspace object is passed to the QWidget not the parent
from KFrame constructor. Parent object is stored in the map provided by
the second KFrame ancestor KItem by calling the :
<B>setKeyData ( PARENT_FRAME, (char*)parent );</B>
where PARENT_FRAME is defined in KFrame.c and parent is an address of
real ( Kondor+ ) parent passed to the constructor of KFrame and usually
used for retrive or set data on/from particular object. If user will want
to use a display parent passed to the QWidget in MDI mode he need to
explicitly call <B>obj->QWidget::parentWidget();</B> in other case the
object stored in KItem will be returned.
@param sameWindow - if true pointer to this object will be returned if there is
no parent otherwise return NULL
@return pointer to parent widget of QWidget type ( usualy it's Kondor+ ) parent to
obtain the data.
/*!
@brief This method overrides parent() from QObject.
code
It will return parent object passed to KFrame in the constructor. This object will not allways
be the same as passed to QWidget (ancestor of KFrame). In the context of
MDI the active workspace object is passed to the QWidget not the parent
from KFrame constructor. Parent object is stored in the map provided by
the second KFrame ancestor KItem by calling the :
<B>setKeyData ( PARENT_FRAME, (char*)parent );</B>
where PARENT_FRAME is defined in KFrame.c and parent is an address of
real ( Kondor+ ) parent passed to the constructor of KFrame and usually
used for retrive or set data on/from particular object. If user will want
to use a display parent passed to the QWidget in MDI mode he need to
explicitly call <B>obj->QWidget::parent();</B> in other case the
object stored in KItem will be returned.
@return pointer to parent widget of QObject type ( usualy it's Kondor+ ) parent to
obtain the data.
/
QObject logicalParent() const;
/*!
This method will set the parent widget in the context of KFrame intrface.
Please note that in context of Qt interface QWidget::parent will be still kept.
*/
void setLogicalParent(QWidget *parent);
/*!
@brief Generic print function for a frame. (write in fd, postscript script).
Prints all activate and non null items which are contained in shown panel.
It also prints out active and not shown tab widget.
code
@return true if something has been printed.
*/
bool print(FILE *fd);
/**
@brief lock the adjustSize fct, call releaseAdjustSize to unlock
@return true if the frame adjustSize is already lock
*/
bool freezeAdjustSize();
/**
@brief unlock adjustSize fct, if adjustSize fonction was called during the lock,
adjustSize is called here.
@param alreadyFreezed if true do nothing, must be the return value of freezeAdjustSize()
*/
void releaseAdjustSize(bool alreadyFreezed);
public:
void recomputeLayout(); //use with caution !!!
bool hasOwnTabOrder();
public:
/**
* @brief - Dump the window content with all widget/value couples.
* This function is used for automatic tests.
*/
std::string dumpContentAsXml() const;
private:
// Helper - Recursively sets/unsets the hourglass cursor on a widget
static void setWaitCursor( QWidget* widget, bool wait );
void showElement();
void createLayout(QWidgetList& widgets);
code
// Callback called when the window is closed
callback_type closeCB_;
close_if_type closeIfCB_;
// Interpose Function
Notify_func interposeFunc_;
// Statusbar
QStatusBar* statusbar_;
// Table id
int tableid_;
code
//Pushpin boolean
bool pushpin_;
// ID - TO REMOVE
int id_;
bool busy_;
code
// Use to report Manager
KFrame * frameSetupReport_;
bool modified_;
KFrame::KFrame(QWidget* parent, const char* name)
: QWidget(nullptr, Qt::Window )
, popup(this)
, closeCB_(nullptr)
, closeIfCB_(nullptr)
, interposeFunc_(nullptr)
, statusbar_(nullptr)
, tableid_(0)
, pushpin_(false)
, id_(0) // TODO: remove
, busy_(0)
, frameSetupReport_(nullptr)
, modified_(false)
, qscrollview_(nullptr)
, startX_(0)
, startY_(0)
, adjustSizeFreezed_(false)
, adjustSizeRequested_(false)
, setBusyLock_(false)
{
// By default we take the font provided by the QApplication.
// This font is set in Xview_init function.
// If this was to be overridden in the UI file or by code,
// this would be a development mistake and a business justification
// would have to be given, otherwise...
setObjectName(name ? name : "unnamed" );
code
setInstance(this);
//Mandatory for Report Dialogs at least
setKeyData( PFRAME, (char*)parent );
// Ensure the background color is set
KQApplication* kapp = dynamic_cast<KQApplication*>(qApp);
if(kapp)
{
QPalette palette;
palette.setColor(this->backgroundRole(), QColor(kapp->dlgBackgroundColor_));
this->setPalette(palette);
}
// Plug a spy to trap Ctrl+R events
// DON'T CHANGE THE FILTERS ORDER
ClearRequestFilter* myFilter = new ClearRequestFilter();
this->installEventFilter(myFilter);
this->installEventFilter( new Size_Position_Filter( this ));
this->installEventFilter( new StateFilter(this));
setWindowIcon(QPixmap(TurazLogo)); // to ensure that all KFrames will display the right logo even if setWindowFlags is called on it...
// In popup mode, we want to ensure the current dialog
// will be displayed on top of its parent to increase usability.
// In case of no parent, the screen of the active dialog is used.
if (parent)
{
this->move(parent->x(), parent->y());
}
else
{
const int K_DIALOG_OFFSET = 10;
QWidget* referenceDlg = qApp->activeWindow(); // Plan B
if (nullptr == referenceDlg) // Plan C
{
referenceDlg = qApp->mainWidget();
}
QDesktopWidget* deskTop = QApplication::desktop();
QRect activeScreenGeo = deskTop->screenGeometry(referenceDlg);
this->move(activeScreenGeo.x() + K_DIALOG_OFFSET, activeScreenGeo.y() + K_DIALOG_OFFSET);
}
QWidget::setAttribute( Qt::WA_WState_Hidden );
/** High dpi - force instrument rescale **/
this->setProperty("HIGH_DPI_DO_RESCALE", true);
void KFrame::init() {
// Now is the right time to find all our children and
// force them to find out what is their instance.
// Avoids random crashes later on when said children are
// reparented (and their instance is supposed to be the one
// at construction time).
QWidgetList widgets = KGlobal::findDirectChildren<QWidget *>(this);
if (!statusbar_ ) {
statusbar_ = new QStatusBar(this, "frame_footer");
statusbar_->setAutoFillBackground(true);
statusbar_->setSizeGripEnabled(isResizable());
statusbar_->adjustSize();
// Qt3-like behavior: activate layout.
if (layout()) {
// Save hidden state and make all children visible
QList<bool> hiddenStates;
for(int i = 0; i< widgets.count(); ++i) {
hiddenStates << widgets.at(i)->isHidden();
widgets.at(i)->setHidden(false);
}
// Activate the layout so that we've meaningful
// size and positions to make the sorting on.
// We do it here for compat with Qt3, which triggered it from adjustSize (in KDRT-70),
// but not if the statusbar already existed (KDRT-179).
// Restore hidden state
for(int i = 0; i< widgets.count(); ++i) {
widgets.at(i)->setHidden( hiddenStates.at(i) );
}
}
// Add the size of the status bar for fixed-size windows
if (!isResizable())
setFixedSize(width(), height() + statusbar_->height());
statusbar_->setMaximumHeight(statusbar_->height());
}
// Create a layout for resizable frames...
if (isResizable())
{
createLayout(widgets);
}
}
}
void KFrame::recomputeLayout()
{
if (layout())
{
return;
}
#ifdef DEBUG_WIDGET_SORTING
qDebug() << "About to sort the children of" << (void*)this << ":";
Q_FOREACH(QWidget* w, directWidgetsChild) {
qDebug() << " " << (void*)w << w->metaObject()->className() << w->objectName() << "y=" << w->y();
}
#endif
// Do the sorting
VerticalWidgetList sorted_widgets(directWidgetsChild);
sorted_widgets.sort();
#ifdef DEBUG_WIDGET_SORTING
// Reflect the layout-insertion order: statusbar is last
directWidgetsChild = sorted_widgets;
directWidgetsChild.removeAll(statusbar_);
directWidgetsChild.append(statusbar_);
QByteArray ba = qgetenv("WINDOW_DISPLAY_INFO");
if ( !ba.isEmpty() && (ba.toInt() == 1) )
widgetInfo( this );
}
// Duplicated code from Qt just to add the isTopLevel constraint
// This is because we don't want sub windows to be taken into account
// when performing a size adjustment!
QRect KFrame::childrenRect() const
{
QRect r(0, 0, 0, 0);
int statusBarHeight = 0;
if (statusbar_)
statusBarHeight = statusbar_->height();
if(qscrollview_)
{
QRect rect = this->childrenRect();
int svMargin = qscrollview_->frameWidth() *2 ;
QSize dlgSizeHint = QSize(rect.width() +svMargin, rect.height() +svMargin+ statusBarHeight);
QSize contentSizeHint =QSize( rect.width()-2*svMargin,rect.height()-2*svMargin);
setMinimumSize(QSize(0,0));
setMaximumSize(dlgSizeHint);
if (qscrollview_->verticalScrollBar()->isVisible() == false
&& qscrollview_->horizontalScrollBar()->isVisible() == false)
{
resize(dlgSizeHint);
}
qscrollview_->resizeContents(contentSizeHint.width(),contentSizeHint.height());
QVariant variant = property(Kprops_KustomWindowSize);
if (true == variant.isValid() && QVariant::Size == variant.type())
{
QSize size = variant.value<QSize>();
size.setHeight(std::max(size.height(), dlgSizeHint.height()));
size.setWidth(std::max(size.width(), dlgSizeHint.width()));
setMaximumHeight(size.height() + statusBarHeight);
setMaximumWidth(size.width());
if (contentSizeHint.width() < size.width() || contentSizeHint.height() < size.height())
{
qscrollview_->resize(contentSizeHint);
}
resize(size);
}
// workaround for Q3ScrollView bug, which calls ensureVisible without
// updating visibleWidth/visibleHeight first. (KDRT-162)
qscrollview_->updateScrollBars();
}
else
{
if (statusbar_ && !layout()) {
// We recompute the geometry of the frame
// without status bar, and then we add this later // at the very bottom of the frame!
statusbar_->hide();
QRect rect = this->childrenRect();
statusbar_->show();
statusbar_->setGeometry(0, rect.bottom(), width(), statusBarHeight);
resize(rect.width() + 2 * rect.x(), rect.height() + 2 * rect.y()
+ statusBarHeight);
}
else
{
/*
* Frame size should be adjusted only once against sizeHint
* If Size_Position_Filter have geometry stored it should be used
* rather than this mechanism.
*/
if ( !this->property("GEOMETRY_MANAGED").isValid() )
QWidget::adjustSize();
}
}
// Additional use case that occures when the KFrame itself
// is hosted into a QScrollView to fit into the physical screen!
if (qscrollview_)
{
// thism method calls showMaximized method from QWidget interface. According to Trolltech manual :
/*
Shows the widget maximized.
Calling this function only affects top-level widgets.
On X11, this function may not work properly with certain window managers.
See the Window Geometry documentation for an explanation.
*/
adjustScrollable(this);
}
}
bool KFrame::getBusy() {
return busy_;
}
void KFrame::setBusy(bool busy)
{
if(setBusyLock_)
{
/**
* The setBusy method is locked, because an
* event has not finished processing this method,
* and another event was generated in the Q_FOREACH
* loop below.
*/
return;
}
ScopedValueAssigner locked(setBusyLock_, true);
code
if (busy_ == busy)
return;
busy_ = busy;
QWidget *w = nullptr;
if (qscrollview_){
w = qscrollview_->viewport();
} else {
w = this;
}
// Disable all the panels of the widget, but not the widget itself,
// so that we can use the busy cursor (if we disable the top-level widget,
// we won't be able to use it)
QList<KPanel *> panels = KGlobal::findDirectChildren<KPanel *>(w);
Q_FOREACH(KPanel* child, panels) {
child->setBusy(busy);
}
QList<QWidget *> children = KGlobal::findDirectChildren<QWidget *>(w);
Q_FOREACH(QWidget* child, children) {
if (child->inherits("KFramelist")) // can't use qobject_cast, different lib
child->setEnabled(!busy);
}
setWaitCursor(this, busy);
// Reenable the status bar
if (busy) {
if (statusbar_)
statusbar_->setEnabled(true);
}
if (closeIfCB_) {
proceed = closeIfCB_(this);
} else if (closeCB_)
closeCB_(this);
if (1 == proceed)
{
QWidget::closeEvent(event);
}
else {
// in order to prevent the scrollable view
// that may contain the current frame, from being closed!
event->ignore();
}
}
KGUIComponentHeader* KFrame::getHeader() {
QList<QWidget *> list = qFindChildren<QWidget *>(this);
QListIterator<QWidget *> it( list );
/*
The printing mechanism has some "inconsistencies" when panels' items are printed.
When e.g. frame prints it's panels then each panel should embrace contents of controls by:
"[ Panel-Item PANEL_NAME [
...
postscript content written by controls
...
]]"
In the ideal situation this postscript code should be inserted by the same C++ function .
However the opening part of this script ("[ Panel-Item PANEL_NAME [")
is writen to the file by the first control which have any contents because frame
doesn't know if and when to open this tag.
Moreover the tag isn't closed by the panel item object (e.g. KTextEdit object) - only the frame knows if a control
has to print something and when to close the panel's tag after the last child with the use of "]]"
/
bool KFrame::print(FILE fd)
{
std::map< KPanel , std::vector<KPanelItem> > allItemsGroupByDirectParentPanel;
allItemsGroupByDirectParentPanel.clear();
code
//retrieve direct panels children
QWidget* frame = nullptr;
if(qscrollview_)
frame = qscrollview_->viewport();
else
frame = this;
const QList<KPanel*> panelsLst = KGlobal::findDirectChildren<KPanel *>(frame);
Q_FOREACH( KPanel* curPanel, panelsLst )
{
// for each found panel, we retrieve, recursively, its items.
const QList<KPanelItem*>* items = curPanel->queryItems();
if( items )
{
//group items by their panel parent only if the item is active (we don't have to print inactive field)
QListIterator<KPanelItem*> it(*items);
while (it.hasNext())
{
KPanelItem* curItem = it.next();
if (curItem->getPanel() == nullptr)
{
continue;
}
// Append all items in the list because there is no difference between ReadOnly and Disabled entries
std::vector<KPanelItem*> & itemsVector = allItemsGroupByDirectParentPanel[curItem->getPanel()];
itemsVector.push_back(curItem);
}
delete items;
}
}
std::vector<KPanel*> sortedPrintablePanels;
sortedPrintablePanels.clear();
for( std::map< KPanel* , std::vector<KPanelItem*> >::iterator itmap = allItemsGroupByDirectParentPanel.begin();
itmap != allItemsGroupByDirectParentPanel.end();
++itmap )
{
//check if panels containing items are printable (ie it is printable and all its parent panels too )
if( (*itmap).first->hasToBePrint() )
sortedPrintablePanels.push_back((*itmap).first);
}
if ( sortedPrintablePanels.empty() )
return false;
//sort printable panels by their absolute position in the frame
std::sort(sortedPrintablePanels.begin(), sortedPrintablePanels.end(), SortKPanelByPosition());
std::vector<KPanel*>::iterator itPanel = sortedPrintablePanels.begin();
//has something has been printed
bool hasPrinted = false;
for(; itPanel != sortedPrintablePanels.end(); ++itPanel)
{
std::map< KPanel* , std::vector<KPanelItem*> >::iterator itmap = allItemsGroupByDirectParentPanel.find(*itPanel);
if (itmap != allItemsGroupByDirectParentPanel.end())
{
std::sort((*itmap).second.begin(), (*itmap).second.end(), SortKPanelItemByPosition());
int nb = 0;
int op = FALSE;
std::vector<KPanelItem*>::iterator curItem = (*itmap).second.begin();
for(; curItem != (*itmap).second.end(); ++curItem)
{
// if curItem prints anything then panel's tag is opened by this object (see print PanelItem::print implementation)
(*curItem)->print(fd,nb, op);
}
// only if anything has been printed then the frame closes panel's tag
// for KLineEdit this is 0 though but op is 1
if (nb)
{
fprintf(fd," ]\n ] %% End of Panel Item %%\n");
}
if(nb || op)
hasPrinted = true;
}
}
const QList<KTextEdit*> textLst = KGlobal::findDirectChildren<KTextEdit *>(frame);
// print TextEdit content
Q_FOREACH( KTextEdit* curText, textLst )
{
int nb = 0;
int op = FALSE;
curText->print(fd, nb, op);
if (nb) // close control tag only if curText has printed anything
{
fprintf(fd," ]\n ] %% End of TextEdit Panel Item %%\n");
hasPrinted = true;
}
}
return hasPrinted;
}
void
KFrame::mouseMoveEvent(QMouseEvent* e)
{
if ((e->state() == Qt::MidButton)&& qscrollview_)
{
int offsetTempX = e->pos().x() - startX_;
int offsetTempY = e->pos().y() - startY_;
void KFrame::makeShrinkable(bool isLocked, bool isAfakeDialogReplacementCase)
{
if (!qscrollview_ )
{
setMinimumSize(QSize(0,0));
if (statusbar_)
delete statusbar_;
code
statusbar_ = new QStatusBar(this, "frame_footer");
statusbar_->setAutoFillBackground(true);
qscrollview_ = new Q3ScrollView(this, "scroll_view");
qscrollview_->setHScrollBarMode(Q3ScrollView::Auto);
qscrollview_->setVScrollBarMode(Q3ScrollView::Auto);
qscrollview_->setResizePolicy(Q3ScrollView::Manual);
// We want the Q3Scrollview to be totally transparent...
// Replacing it by a QScrollArea would be better, but much more expensive.
qscrollview_->viewport()->setBackgroundColor(this->palette().color(this->backgroundRole()));
QList<QWidget*> widgets = KGlobal::findDirectChildren<QWidget *>(this);
Q_FOREACH(QWidget* obj, widgets)
{
if (!obj->inherits("Q3ScrollView") && !obj->inherits("QMenu")
&& !obj->inherits("Q3Menu") && !obj->inherits("KFrame"))
{
obj->setParent(qscrollview_->viewport());
}
}
if (!layout())
{
QLayout* layout = new QVBoxLayout(this);
layout->setContentsMargins(0,0,0,0);
layout->setSpacing(0);
}
if(qscrollview_)
layout()->add(qscrollview_);
if (statusbar_)
layout()->add(statusbar_);
}
//Do not uncomment this statment because we need the Frame to be no resizable
//setMinimumSize(QSize(0,0));
}
void KFrame::restoreTabOrder() {
qDebug(" TAB ORDER NOT RESTORED for : %s class %s . Reimplement this method in ...ui.h file", ( name() ? name():"UNNAMED" ), className());
}
void KFrame::setModeCustomWindow(bool cwMode)
{
// if Custom Window mode
if (cwMode)
{
int fontSize = 8;
#ifdef WIN32
//37620: Help list from open report may not be readable with font "Terminal"
QString fontName("Courier New");
#else
QString fontName("Bitstream Vera Sans Mono");
fontSize = 7;
#endif
code
// create fixed width font
QFont fixedWidthFont(fontName, fontSize);
// apply font to frame
this->setFont(fixedWidthFont);
}
}
QFont KFrame::setFixedFont()
{
int fontSize = 8;
#ifdef WIN32
QString fontName("Courier New");
#else
QString fontName("Bitstream Vera Sans Mono");
fontSize = 7;
#endif
code
// create fixed width font
QFont fixedWidthFont(fontName, fontSize);
return fixedWidthFont;
if(this->qScrollView())
{
theOldParent = this->qScrollView()->viewport();
//carefull, it is called with KPanel in case of RK instrumentCriteriaChanged
KFrame* frame = qobject_cast<KFrame*>(parent);
if (frame && !parent->layout())
{
parent->makeShrinkable();
}
}
else
{
theOldParent = this;
}
QList<KPanel*> panels = KGlobal::findDirectChildren<KPanel *>(theOldParent);
Q_FOREACH(KPanel* obj, panels)
{
obj->reparent(parent,obj->pos(),true);
obj->setInstance(parent);
}
}
KFrame* KFrame::getByName(const char* name)
{
KFrame* childFrame = nullptr;
QWidgetList list = QApplication::allWidgets();
QWidget * w;
Q_FOREACH(w, list)
{
if (w->inherits("KFrame"))
{
if (!(strcmp(w->name(), name )))
{
childFrame = (KFrame* )w;
break;
}
}
}
return childFrame;
}
KFrame* KFrame::createFrameCMDFull(QWidget* owner,
int footer_flag,
int resize_flag,
int /pin_flag/,
int width,
int height,
void(* /event_proc/)(),
char* cd,
char* buffer
)
{
KFrame* frame = new KFrame(owner, buffer);
code
KPanel* panel = new KPanel(frame,"panel_cmd"); // Do not remove this statement. A panel named panel_cmd is expected in such KFrame.
Q_UNUSED(panel);
// Note: The order of the 3 following statements must be preserved!
frame->resize(width, height);
frame->setResizable(resize_flag);
frame->setStatusBar(footer_flag);
frame->setClientData(cd);
return frame;
}
KFrame* KFrame::createFrame(const char* label, int width, int height, QPixmap* image)
{
KFrame* frame = new KFrame;
frame->setResizable( false );
frame->setStatusBar( true );
frame->modify(label, width, height, image);
I want to use QScrollArea instead Q3ScrollView. Right now all widgets which are on KFrame are reparented to Q3ScrollView and even if KFrame in ui file is not resizable became resizable while makeShrinkable is called. As a result I need to have this KFrmae and whole it's content (except of QScrollBar) placed on QScrollArea and this QScrollArea (with all KFrame content) should be added to QVboxLayout on top and QScrollBar on bottom. Please do the implementation
KFrame.cpp (Implementation File)
Here's how you'd implement the makeShrinkable method, along with other relevant changes:
cpp
#include "KFrame.h"
#include "KGlobal.h"
// Other includes...
KFrame::KFrame(QWidget* parent, const char* name)
: QWidget(nullptr, Qt::Window),
// Initialize other members...
qscrollarea_(nullptr) // Initialize the QScrollArea
{
setObjectName(name ? name : "unnamed");
setInstance(this);
// Ensure the background color is set as before...
// Install event filters if needed...
}
KFrame::~KFrame() {
delete qscrollarea_; // Delete the QScrollArea
delete statusbar_;
}
void KFrame::makeShrinkable(bool isLocked, bool isAfakeDialogReplacementCase) {
if (!qscrollarea_) {
setMinimumSize(QSize(0, 0));
if (statusbar_)
delete statusbar_;
statusbar_ = new QStatusBar(this);
statusbar_->setAutoFillBackground(true);
// Initialize the QScrollArea
qscrollarea_ = new QScrollArea(this);
qscrollarea_->setFrameShape(QFrame::NoFrame); // No frame for transparency
qscrollarea_->setWidgetResizable(true); // Allows the contained widget to resize
// Create a container widget for QScrollArea to hold KFrame's content
QWidget* containerWidget = new QWidget();
QVBoxLayout* layout = new QVBoxLayout(containerWidget);
// Re-parent all direct child widgets to the container widget
QList<QWidget*> widgets = KGlobal::findDirectChildren<QWidget *>(this);
Q_FOREACH(QWidget* obj, widgets) {
if (!obj->inherits("QScrollArea") && !obj->inherits("QMenu") &&
!obj->inherits("KFrame")) {
obj->setParent(containerWidget);
layout->addWidget(obj); // Add to the new layout
}
}
// Set the container widget for the scroll area
qscrollarea_->setWidget(containerWidget);
// Create main layout if it doesn’t already exist
if (!layout()) {
QVBoxLayout* mainLayout = new QVBoxLayout(this);
mainLayout->setContentsMargins(0, 0, 0, 0);
mainLayout->setSpacing(0);
setLayout(mainLayout);
}
// Add the scroll area and status bar to the main layout
layout()->addWidget(qscrollarea_);
if (statusbar_) {
layout()->addWidget(statusbar_);
}
}
}
// Rest of the original methods...
Key Changes:
Use of QScrollArea: The class now uses QScrollArea instead of Q3ScrollView, altering the type from Q3ScrollView* qscrollview_ to QScrollArea* qscrollarea_.
Container Widget: Introduced a new container widget that holds all other widgets from KFrame so that they can be managed properly within the QScrollArea.
Widget Parentage: All direct children of KFrame are properly reparented to the container widget before being added to the QScrollArea.
Layout Management: A new layout is created inside the container widget for managing the positions of contained widgets. This allows for dynamic resizing and management of the child widgets.
Safe Handling of Status Bar: The existing logic has been maintained for handling the QStatusBar in conjunction with QScrollArea.
Additional Considerations:
Adjust any remaining functions or logic that may reference scroll-related functionalities in KFrame to align with how QScrollArea operates.
Ensure that if there are any custom interactions (like mouse movements or double-clicks) that depend on child widgets, they are tested after transitioning to QScrollArea.
The header file can include other tools if required for managing panels or other GUI components typically used in your application.
This new implementation should closely match the original functionality while utilizing QScrollArea, offering better performance and maintainability in a Qt4 environment.