* Added field fKey to BaseCap (will be required for Gutenprint printer

driver add-on).
* Added ability to search for a PrinterCap by ID to class PrinterCap
  (for Gutenprint driver add-on).
* Moved code for searching a PrinterCap by name into class PrinterCap.
* Refactored code in JobSetupDlg to use the new method.
* Refactored duplicated code in JobSetupDlg.
* There is still a lot of refactoring potential in libprint.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@39141 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Michael Pfeiffer 2010-10-25 14:21:09 +00:00
parent 08d759feae
commit f53abcbdf8
9 changed files with 288 additions and 156 deletions

View File

@ -12,6 +12,7 @@
#include "JobData.h"
#include "Halftone.h"
#include "JSDSlider.h"
#include "PrinterCap.h"
class BTextControl;
class BTextView;
@ -34,13 +35,20 @@ public:
private:
void UpdateButtonEnabledState();
void FillCapabilityMenu(BPopUpMenu* menu, uint32 message,
PrinterCap::CapID category, int id);
void FillCapabilityMenu(BPopUpMenu* menu, uint32 message,
const BaseCap** capabilities, int count, int id);
int GetID(const BaseCap** capabilities, int count, const char* label,
int defaultValue);
BRadioButton* CreatePageSelectionItem(const char* name, const char* label,
JobData::PageSelection pageSelection);
void AllowOnlyDigits(BTextView* textView, int maxDigits);
JobData::Color getColor();
Halftone::DitherType getDitherType();
float getGamma();
float getInkDensity();
JobData::Color Color();
Halftone::DitherType DitherType();
float Gamma();
float InkDensity();
JobData::PaperSource PaperSource();
BTextControl *fCopies;
BTextControl *fFromPage;

View File

@ -18,9 +18,13 @@ enum {
struct BaseCap {
BaseCap(const string &label, bool isDefault);
virtual ~BaseCap();
virtual int ID() const = 0;
string fLabel;
bool fIsDefault;
string fKey;
};
struct PaperCap : public BaseCap {
@ -28,6 +32,8 @@ struct PaperCap : public BaseCap {
JobData::Paper paper, const BRect &paperRect,
const BRect &physicalRect);
int ID() const;
JobData::Paper fPaper;
BRect fPaperRect;
BRect fPhysicalRect;
@ -37,13 +43,18 @@ struct PaperSourceCap : public BaseCap {
PaperSourceCap(const string &label, bool isDefault,
JobData::PaperSource paperSource);
int ID() const;
JobData::PaperSource fPaperSource;
};
struct ResolutionCap : public BaseCap {
ResolutionCap(const string &label, bool isDefault,
int xResolution, int yResolution);
int id, int xResolution, int yResolution);
int ID() const;
int fID;
int fXResolution;
int fYResolution;
};
@ -52,6 +63,8 @@ struct OrientationCap : public BaseCap {
OrientationCap(const string &label, bool isDefault,
JobData::Orientation orientation);
int ID() const;
JobData::Orientation fOrientation;
};
@ -59,6 +72,8 @@ struct PrintStyleCap : public BaseCap {
PrintStyleCap(const string &label, bool isDefault,
JobData::PrintStyle printStyle);
int ID() const;
JobData::PrintStyle fPrintStyle;
};
@ -67,6 +82,8 @@ struct BindingLocationCap : public BaseCap {
bool isDefault,
JobData::BindingLocation bindingLocation);
int ID() const;
JobData::BindingLocation fBindingLocation;
};
@ -74,6 +91,8 @@ struct ColorCap : public BaseCap {
ColorCap(const string &label, bool isDefault,
JobData::Color color);
int ID() const;
JobData::Color fColor;
};
@ -82,6 +101,8 @@ struct ProtocolClassCap : public BaseCap {
bool isDefault, int protocolClass,
const string &description);
int ID() const;
int fProtocolClass;
string fDescription;
};
@ -108,16 +129,19 @@ public:
kCopyCommand, // supports printer page copy command?
};
virtual int countCap(CapID) const = 0;
virtual bool isSupport(CapID) const = 0;
virtual const BaseCap** enumCap(CapID) const = 0;
const BaseCap* getDefaultCap(CapID) const;
virtual int countCap(CapID category) const = 0;
virtual bool isSupport(CapID category) const = 0;
virtual const BaseCap** enumCap(CapID category) const = 0;
const BaseCap* getDefaultCap(CapID category) const;
const BaseCap* findCap(CapID category, int id) const;
const BaseCap* findCap(CapID category, const char* label) const;
int getPrinterId() const;
int getProtocolClass() const;
protected:
PrinterCap(const PrinterCap &);
PrinterCap& operator=(const PrinterCap &);
PrinterCap(const PrinterCap &printerCap);
PrinterCap& operator=(const PrinterCap &printerCap);
const PrinterData* getPrinterData() const;
void setPrinterId(int id);

View File

@ -71,7 +71,7 @@ const PaperSourceCap manual("Manual", false, JobData::kManual);
const PaperSourceCap upper("Upper", false, JobData::kUpper);
const PaperSourceCap lower("Lower", false, JobData::kLower);
const ResolutionCap dpi300("300dpi", true, 300, 300);
const ResolutionCap dpi300("300dpi", true, 0, 300, 300);
const PaperCap* papers[] = {
&a4,

View File

@ -98,9 +98,9 @@ const PaperSourceCap upper("Upper", false, JobData::kUpper);
const PaperSourceCap middle("Middle", false, JobData::kMiddle);
const PaperSourceCap lower("Lower", false, JobData::kLower);
const ResolutionCap dpi1200("1200dpi", false, 1200, 1200);
const ResolutionCap dpi600("600dpi", true, 600, 600);
const ResolutionCap dpi300("300dpi", false, 300, 300);
const ResolutionCap dpi1200("1200dpi", false, 0, 1200, 1200);
const ResolutionCap dpi600("600dpi", true, 1, 600, 600);
const ResolutionCap dpi300("300dpi", false, 2, 300, 300);
const PrintStyleCap simplex("Simplex", true, JobData::kSimplex);
const PrintStyleCap duplex("Duplex", false, JobData::kDuplex);

View File

@ -68,9 +68,9 @@ const PaperCap legal(
const PaperSourceCap autobin("Auto", true, JobData::kAuto);
const ResolutionCap dpi300("300dpi", true, 300, 300);
const ResolutionCap dpi600("600dpi", false, 600, 600);
const ResolutionCap dpi1200("1200dpi", false, 1200, 1200);
const ResolutionCap dpi300("300dpi", true, 0, 300, 300);
const ResolutionCap dpi600("600dpi", false, 1, 600, 600);
const ResolutionCap dpi1200("1200dpi", false, 2, 1200, 1200);
const PaperCap* papers[] = {
&a4,

View File

@ -145,10 +145,10 @@ const PaperSourceCap envelopeTray("Envelope Tray", false,
// since 2.0:
const PaperSourceCap thridCassette("Thrid Cassette", false, JobData::kMiddle);
const ResolutionCap dpi150("150dpi", false, 150, 150);
const ResolutionCap dpi300("300dpi", true, 300, 300);
const ResolutionCap dpi600("600dpi", false, 600, 600);
const ResolutionCap dpi1200("1200dpi", false, 1200, 1200);
const ResolutionCap dpi150("150dpi", false, 0, 150, 150);
const ResolutionCap dpi300("300dpi", true, 1, 300, 300);
const ResolutionCap dpi600("600dpi", false, 2, 600, 600);
const ResolutionCap dpi1200("1200dpi", false, 3, 1200, 1200);
const PrintStyleCap simplex("Simplex", true, JobData::kSimplex);
const PrintStyleCap duplex("Duplex", false, JobData::kDuplex);

View File

@ -68,9 +68,9 @@ const PaperCap legal(
const PaperSourceCap autobin("Auto", true, JobData::kAuto);
const ResolutionCap dpi300("300dpi", true, 300, 300);
const ResolutionCap dpi600("600dpi", false, 600, 600);
const ResolutionCap dpi1200("1200dpi", false, 1200, 1200);
const ResolutionCap dpi300("300dpi", true, 0, 300, 300);
const ResolutionCap dpi600("600dpi", false, 1, 600, 600);
const ResolutionCap dpi1200("1200dpi", false, 2, 1200, 1200);
const PaperCap* papers[] = {
&a4,

View File

@ -56,6 +56,8 @@ struct NupCap : public BaseCap {
fNup(nup)
{}
int ID() const { return fNup; }
int fNup;
};
@ -68,6 +70,8 @@ struct DitherCap : public BaseCap {
fDitherType(ditherType)
{}
int ID() const { return fDitherType; }
Halftone::DitherType fDitherType;
};
@ -90,7 +94,7 @@ static const DitherCap gDitherFloydSteinberg("Floyd-Steinberg", false,
Halftone::kTypeFloydSteinberg);
const NupCap *gNups[] = {
const BaseCap *gNups[] = {
&gNup1,
&gNup2,
&gNup4,
@ -103,7 +107,7 @@ const NupCap *gNups[] = {
};
const DitherCap *gDitherTypes[] = {
const BaseCap *gDitherTypes[] = {
&gDitherType1,
&gDitherType2,
&gDitherType3,
@ -121,6 +125,7 @@ enum {
kMsgCollateChanged,
kMsgReverseChanged,
kMsgDuplexChanged,
kMsgNone = 0
};
@ -172,48 +177,16 @@ JobSetupView::AttachedToWindow()
// color
fColorType = new BPopUpMenu("color");
fColorType->SetRadioMode(true);
int count = fPrinterCap->countCap(PrinterCap::kColor);
const ColorCap **color_cap = (const ColorCap **)fPrinterCap->enumCap(
PrinterCap::kColor);
bool marked = false;
BMenuItem* item = NULL;
while (count--) {
item = new BMenuItem((*color_cap)->fLabel.c_str(),
new BMessage(kMsgQuality));
fColorType->AddItem(item);
if ((*color_cap)->fColor == fJobData->getColor()) {
item->SetMarked(true);
marked = true;
}
color_cap++;
}
if (!marked && item)
item->SetMarked(true);
FillCapabilityMenu(fColorType, kMsgQuality, PrinterCap::kColor,
fJobData->getColor());
BMenuField* colorMenuField = new BMenuField("color", "Color:", fColorType);
fColorType->SetTargetForItems(this);
// dither type
fDitherType = new BPopUpMenu("");
fDitherType->SetRadioMode(true);
count = sizeof(gDitherTypes) / sizeof(gDitherTypes[0]);
const DitherCap **dither_cap = gDitherTypes;
marked = false;
item = NULL;
while (count--) {
item = new BMenuItem((*dither_cap)->fLabel.c_str(),
new BMessage(kMsgQuality));
fDitherType->AddItem(item);
if ((*dither_cap)->fDitherType == fJobData->getDitherType()) {
item->SetMarked(true);
marked = true;
}
dither_cap++;
}
if (!marked && item)
item->SetMarked(true);
FillCapabilityMenu(fDitherType, kMsgQuality, gDitherTypes, sizeof(gDitherTypes) /
sizeof(gDitherTypes[0]), fJobData->getDitherType());
BMenuField* ditherMenuField = new BMenuField("dithering", "Dot Pattern:",
fDitherType);
fDitherType->SetTargetForItems(this);
@ -297,43 +270,16 @@ JobSetupView::AttachedToWindow()
// paper source
fPaperFeed = new BPopUpMenu("");
fPaperFeed->SetRadioMode(true);
count = fPrinterCap->countCap(PrinterCap::kPaperSource);
const PaperSourceCap **paper_source_cap =
(const PaperSourceCap **)fPrinterCap->enumCap(PrinterCap::kPaperSource);
marked = false;
item = NULL;
while (count--) {
item = new BMenuItem((*paper_source_cap)->fLabel.c_str(), NULL);
fPaperFeed->AddItem(item);
if ((*paper_source_cap)->fPaperSource == fJobData->getPaperSource()) {
item->SetMarked(true);
marked = true;
}
paper_source_cap++;
}
if (!marked)
item->SetMarked(true);
FillCapabilityMenu(fPaperFeed, kMsgNone, PrinterCap::kPaperSource,
fJobData->getPaperSource());
BMenuField* paperSourceMenufield = new BMenuField("paperSource",
"Paper Source:", fPaperFeed);
// Pages per sheet
fNup = new BPopUpMenu("");
fNup->SetRadioMode(true);
count = sizeof(gNups) / sizeof(gNups[0]);
const NupCap **nup_cap = gNups;
marked = false;
item = NULL;
while (count--) {
item = new BMenuItem((*nup_cap)->fLabel.c_str(), NULL);
fNup->AddItem(item);
if ((*nup_cap)->fNup == fJobData->getNup()) {
item->SetMarked(true);
marked = true;
}
nup_cap++;
}
if (!marked)
item->SetMarked(true);
FillCapabilityMenu(fNup, kMsgNone, gNups, sizeof(gNups) / sizeof(gNups[0]),
fJobData->getNup());
BMenuField* pagesPerSheet = new BMenuField("pagesPerSheet",
"Pages Per Sheet:", fNup);
@ -504,6 +450,72 @@ JobSetupView::AttachedToWindow()
}
void
JobSetupView::FillCapabilityMenu(BPopUpMenu* menu, uint32 message,
PrinterCap::CapID category, int id)
{
int count = fPrinterCap->countCap(category);
const BaseCap **capabilities = fPrinterCap->enumCap(category);
FillCapabilityMenu(menu, message, capabilities, count, id);
}
void
JobSetupView::FillCapabilityMenu(BPopUpMenu* menu, uint32 message,
const BaseCap** capabilities, int count, int id)
{
bool marked = false;
BMenuItem* firstItem = NULL;
BMenuItem* defaultItem = NULL;
BMenuItem* item = NULL;
while (count--) {
const BaseCap* capability = *capabilities;
if (message != kMsgNone)
item = new BMenuItem(capability->fLabel.c_str(),
new BMessage(kMsgQuality));
else
item = new BMenuItem(capability->fLabel.c_str(), NULL);
menu->AddItem(item);
if (firstItem == NULL)
firstItem = item;
if (capability->fIsDefault)
defaultItem = item;
if (capability->ID() == id) {
item->SetMarked(true);
marked = true;
}
capabilities++;
}
if (marked)
return;
if (defaultItem != NULL)
defaultItem->SetMarked(true);
else if (firstItem != NULL)
firstItem->SetMarked(true);
}
int
JobSetupView::GetID(const BaseCap** capabilities, int count, const char* label,
int defaultValue)
{
while (count--) {
const BaseCap* capability = *capabilities;
if (capability->fLabel == label)
return capability->ID();
}
return defaultValue;
}
void
JobSetupView::UpdateButtonEnabledState()
{
@ -530,7 +542,8 @@ JobSetupView::MessageReceived(BMessage *msg)
break;
case kMsgQuality:
fHalftone->preview(getGamma(), getInkDensity(), getDitherType(), getColor() != JobData::kMonochrome);
fHalftone->preview(Gamma(), InkDensity(), DitherType(),
Color() != JobData::kMonochrome);
break;
case kMsgCollateChanged:
@ -545,38 +558,29 @@ JobSetupView::MessageReceived(BMessage *msg)
JobData::Color
JobSetupView::getColor()
JobSetupView::Color()
{
int count = fPrinterCap->countCap(PrinterCap::kColor);
const ColorCap **color_cap = (const ColorCap**)fPrinterCap->enumCap(PrinterCap::kColor);
const char *color_label = fColorType->FindMarked()->Label();
while (count--) {
if (!strcmp((*color_cap)->fLabel.c_str(), color_label)) {
return (*color_cap)->fColor;
}
color_cap++;
}
return JobData::kMonochrome;
const char *label = fColorType->FindMarked()->Label();
const BaseCap* capability = fPrinterCap->findCap(PrinterCap::kColor, label);
if (capability == NULL)
return JobData::kMonochrome;
const ColorCap* colorCap = static_cast<const ColorCap*>(capability);
return colorCap->fColor;
}
Halftone::DitherType
JobSetupView::getDitherType()
JobSetupView::DitherType()
{
int count = sizeof(gDitherTypes) / sizeof(gDitherTypes[0]);
const DitherCap **dither_cap = gDitherTypes;
const char *dithering_label = fDitherType->FindMarked()->Label();
while (count --) {
if (strcmp((*dither_cap)->fLabel.c_str(), dithering_label) == 0) {
return (*dither_cap)->fDitherType;
}
dither_cap ++;
}
return Halftone::kTypeFloydSteinberg;
const char *label = fDitherType->FindMarked()->Label();
int id = GetID(gDitherTypes, sizeof(gDitherTypes) / sizeof(gDitherTypes[0]),
label, Halftone::kTypeFloydSteinberg);
return static_cast<Halftone::DitherType>(id);
}
float
JobSetupView::getGamma()
JobSetupView::Gamma()
{
const float value = (float)fGamma->Value();
return pow(2.0, value / 100.0);
@ -584,23 +588,34 @@ JobSetupView::getGamma()
float
JobSetupView::getInkDensity()
JobSetupView::InkDensity()
{
const float value = (float)(127 - fInkDensity->Value());
return value;
}
JobData::PaperSource
JobSetupView::PaperSource()
{
const char *label = fPaperFeed->FindMarked()->Label();
const BaseCap* capability = fPrinterCap->findCap(PrinterCap::kPaperSource,
label);
if (capability == NULL)
capability = fPrinterCap->getDefaultCap(PrinterCap::kPaperSource);
return static_cast<const PaperSourceCap*>(capability)->fPaperSource;
}
bool
JobSetupView::UpdateJobData(bool showPreview)
{
int count;
fJobData->setShowPreview(showPreview);
fJobData->setColor(getColor());
fJobData->setGamma(getGamma());
fJobData->setInkDensity(getInkDensity());
fJobData->setDitherType(getDitherType());
fJobData->setColor(Color());
fJobData->setGamma(Gamma());
fJobData->setInkDensity(InkDensity());
fJobData->setDitherType(DitherType());
int first_page;
int last_page;
@ -616,30 +631,14 @@ JobSetupView::UpdateJobData(bool showPreview)
fJobData->setFirstPage(first_page);
fJobData->setLastPage(last_page);
count = fPrinterCap->countCap(PrinterCap::kPaperSource);
const PaperSourceCap **paper_source_cap = (const PaperSourceCap **)fPrinterCap->enumCap(PrinterCap::kPaperSource);
const char *paper_source_label = fPaperFeed->FindMarked()->Label();
while (count--) {
if (!strcmp((*paper_source_cap)->fLabel.c_str(), paper_source_label)) {
fJobData->setPaperSource((*paper_source_cap)->fPaperSource);
break;
}
paper_source_cap++;
}
fJobData->setPaperSource(PaperSource());
count = sizeof(gNups) / sizeof(gNups[0]);
const NupCap **nup_cap = gNups;
const char *nup_label = fNup->FindMarked()->Label();
while (count--) {
if (!strcmp((*nup_cap)->fLabel.c_str(), nup_label)) {
fJobData->setNup((*nup_cap)->fNup);
break;
}
nup_cap++;
}
fJobData->setNup(GetID(gNups, sizeof(gNups) / sizeof(gNups[0]),
fNup->FindMarked()->Label(), 1));
if (fPrinterCap->isSupport(PrinterCap::kPrintStyle)) {
fJobData->setPrintStyle((B_CONTROL_ON == fDuplex->Value()) ? JobData::kDuplex : JobData::kSimplex);
fJobData->setPrintStyle((B_CONTROL_ON == fDuplex->Value())
? JobData::kDuplex : JobData::kSimplex);
}
fJobData->setCopies(atoi(fCopies->Text()));

View File

@ -14,6 +14,11 @@ BaseCap::BaseCap(const string &label, bool isDefault)
}
BaseCap::~BaseCap()
{
}
PaperCap::PaperCap(const string &label, bool isDefault, JobData::Paper paper,
const BRect &paperRect, const BRect &physicalRect)
:
@ -25,6 +30,13 @@ PaperCap::PaperCap(const string &label, bool isDefault, JobData::Paper paper,
}
int
PaperCap::ID() const
{
return fPaper;
}
PaperSourceCap::PaperSourceCap(const string &label, bool isDefault,
JobData::PaperSource paperSource)
:
@ -34,16 +46,31 @@ PaperSourceCap::PaperSourceCap(const string &label, bool isDefault,
}
int
PaperSourceCap::ID() const
{
return fPaperSource;
}
ResolutionCap::ResolutionCap(const string &label, bool isDefault,
int xResolution, int yResolution)
int id, int xResolution, int yResolution)
:
BaseCap(label, isDefault),
fID(id),
fXResolution(xResolution),
fYResolution(yResolution)
{
}
int
ResolutionCap::ID() const
{
return fID;
}
OrientationCap::OrientationCap(const string &label, bool isDefault,
JobData::Orientation orientation)
:
@ -53,6 +80,13 @@ OrientationCap::OrientationCap(const string &label, bool isDefault,
}
int
OrientationCap::ID() const
{
return fOrientation;
}
PrintStyleCap::PrintStyleCap(const string &label, bool isDefault,
JobData::PrintStyle printStyle)
:
@ -62,6 +96,13 @@ PrintStyleCap::PrintStyleCap(const string &label, bool isDefault,
}
int
PrintStyleCap::ID() const
{
return fPrintStyle;
}
BindingLocationCap::BindingLocationCap(const string &label, bool isDefault,
JobData::BindingLocation bindingLocation)
:
@ -71,6 +112,13 @@ BindingLocationCap::BindingLocationCap(const string &label, bool isDefault,
}
int
BindingLocationCap::ID() const
{
return fBindingLocation;
}
ColorCap::ColorCap(const string &label, bool isDefault, JobData::Color color)
:
BaseCap(label, isDefault),
@ -79,6 +127,13 @@ ColorCap::ColorCap(const string &label, bool isDefault, JobData::Color color)
}
int
ColorCap::ID() const
{
return fColor;
}
ProtocolClassCap::ProtocolClassCap(const string &label, bool isDefault,
int protocolClass, const string &description)
:
@ -89,6 +144,13 @@ ProtocolClassCap::ProtocolClassCap(const string &label, bool isDefault,
}
int
ProtocolClassCap::ID() const
{
return fProtocolClass;
}
PrinterCap::PrinterCap(const PrinterData *printer_data)
: fPrinterData(printer_data),
fPrinterID(kUnknownPrinter)
@ -101,22 +163,61 @@ PrinterCap::~PrinterCap()
}
const BaseCap *PrinterCap::getDefaultCap(CapID id) const
const BaseCap *
PrinterCap::getDefaultCap(CapID category) const
{
int count = countCap(id);
if (count > 0) {
const BaseCap **base_cap = enumCap(id);
while (count--) {
if ((*base_cap)->fIsDefault) {
return *base_cap;
}
base_cap++;
int count = countCap(category);
if (count <= 0)
return NULL;
const BaseCap **base_cap = enumCap(category);
while (count--) {
if ((*base_cap)->fIsDefault) {
return *base_cap;
}
base_cap++;
}
return enumCap(category)[0];
}
const BaseCap*
PrinterCap::findCap(CapID category, int id) const
{
int count = countCap(category);
if (count <= 0)
return NULL;
const BaseCap **base_cap = enumCap(category);
while (count--) {
if ((*base_cap)->ID() == id) {
return *base_cap;
}
base_cap++;
}
return NULL;
}
const BaseCap*
PrinterCap::findCap(CapID category, const char* label) const
{
int count = countCap(category);
if (count <= 0)
return NULL;
const BaseCap **base_cap = enumCap(category);
while (count--) {
if ((*base_cap)->fLabel == label) {
return *base_cap;
}
base_cap++;
}
return NULL;
}
int
PrinterCap::getProtocolClass() const {
return fPrinterData->getProtocolClass();